我试图沿着曲线移动一个游戏对象。除此之外,我想相应地改变那个游戏对象的角度。
以下是我的代码。但它不能正常工作:
公共类CharacterController:MonoBehaviour {
Vector3 prevPos, currentPos;
Vector3 perpPos;
// Use this for initialization
void Start () {
Debug.Log ("Character Controller start method");
prevPos = gameObject.transform.position;
currentPos = gameObject.transform.position;
}
// Update is called once per frame
void Update () {
currentPos = this.transform.position;
perpPos = new Vector3 (currentPos.x, prevPos.y, 0.0f);
//Debug.Log ("perpPos" + perpPos);
float perpDist = Vector3.Distance (perpPos, currentPos);
float baseDist = Vector3.Distance (prevPos, perpPos);
//Debug.Log ("perpDist " + perpDist);
//Debug.Log ("baseDist " + baseDist);
float angle = Mathf.Tan (perpDist / baseDist);
angle = angle * Mathf.Rad2Deg;
Debug.Log ("angle " + angle);
gameObject.transform.rotation = Quaternion.Euler (0,0, angle);
this.transform.eulerAngles = new Vector3 (0, 0, angle);
//Debug.Log ("angle " + angle);
//Quaternion rot=new Quaternion();
//rot.eulerAngles = new Vector3(0, 0, angle);
//transform.rotation=rot;
prevPos = currentPos;
}
}
在这里,我获得了游戏对象的先前位置和当前位置。计算第3个点,这样我就可以得到2个单独向量的垂直和基数。从那里我使用tan(Theta)= perp / base公式计算角度。但这似乎并不顺利。
如果有人可以帮忙吗?