箭矢在3d世界做抛物线
程序员文章站
2022-04-02 09:39:01
...
这里使用的是模拟重力的方式,来使物体做抛物线运动,同时画出运动轨迹;当然,这里的代码是摘抄的两篇文章里的,在最后有两篇文章的连接,各位童鞋自行去看;
我主要说明一下,因为模拟重力做抛物线运动,这个没有问题,最主要的是如果物体是个箭矢(射箭的);那么这个箭矢的角度问题如何控制,很遗憾,没有做出来,不过以下代码只需要控制号好角度就能用:
x -90至90度
y -45至45度
只要保证箭矢的角度在以上范围内,就正常,否则,就会出现不可名状的状况,好了,这就是我要说明的,欢迎大神指正
public class TestRadar2 : MonoBehaviour {
public float Power = 10;//这个代表发射时的速度/力度等,
//public float Angle = 45;//发射的角度
public float Gravity = -10;//这个代表重力加速度
public bool IsOne = false;
private Vector3 MoveSpeed;//初速度向量
private Vector3 GritySpeed = Vector3.zero;//重力的速度向量,t时为0
private float dTime;//已经过去的时间
private Vector3 currentAngle;
Material m_LineMat;
//点集合
public List<Vector3> m_List = new List<Vector3>();
public List<Vector3> tempm_List = new List<Vector3>();
private float timer;
public bool isShoot;
public int index;
void Start()
{
//MoveSpeed = Quaternion.Euler(new Vector3(0, 0, Angle)) * Vector3.right * Power;
MoveSpeed = Quaternion.Euler(transform.right) * transform.forward * Power;
//currentAngle = Vector3.zero;
Debug.Log(transform.eulerAngles + " " + transform.localEulerAngles + " " + transform.rotation);
currentAngle = transform.eulerAngles;
ChangeAngle();
}
public void ChangeAngle()
{
}
// Update is called once per frame
void FixedUpdate()
{
//计算物体的重力速度
//v = at ;
GritySpeed.y = Gravity * (dTime += Time.fixedDeltaTime);
//位移模拟轨迹
transform.position += (MoveSpeed + GritySpeed) * Time.fixedDeltaTime;
timer += Time.fixedDeltaTime;
if (timer > 0.1f)
{
timer = 0;
tempm_List.Add(transform.position);
m_List = tempm_List;
//Debug.Log(m_List.Count + " " + tempm_List.Count);
}
currentAngle.x = -Mathf.Atan((MoveSpeed.y + GritySpeed.y) / MoveSpeed.z) * Mathf.Rad2Deg;
Debug.Log(currentAngle.x);
transform.eulerAngles = currentAngle;
}
private void OnRenderObject()
{
CreateLineMaterial();
//通过一个公式计算出初速度向量
//角度*力度
int i = 0, iMax = 0;
m_LineMat.SetPass(0);
GL.Begin(GL.LINES);
i = 0;
//m_List = tempm_List;
iMax = m_List.Count;
for (i = 0; i < iMax; i++)
{
//Debug.Log(1221);
GL.Vertex(m_List[i]);
}
GL.End();
//m_List.Clear();
}
void CreateLineMaterial()
{
if (!m_LineMat)
{
//Debug.Log(m_LineMat.name);
var shader = Shader.Find("Hidden/Internal-Colored");
m_LineMat = new Material(shader);
m_LineMat.hideFlags = HideFlags.HideAndDontSave;
m_LineMat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
m_LineMat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
m_LineMat.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
m_LineMat.SetInt("_ZWrite", 0);
}
}
}
抛物线:
http://www.cnblogs.com/jqg-aliang/p/4806002.html
画线:
http://blog.csdn.net/UtilXK/article/details/51577399
推荐阅读