您的位置:首页 > 移动开发 > Unity3D

unity 一个物体相对另一个物体坐标系的偏转角度计算

2016-06-17 15:43 459 查看
using UnityEngine;
using System.Collections;

public class UserAnimatorMove : MonoBehaviour
{

private Vector3 oldVector3;
private Vector3 newVector3;
private Quaternion oldQuaternion;
private Vector3 newRotatinForward;
void Start()
{

}
void Update()
{
if (Input.GetKeyDown(KeyCode.F1))
{

oldVector3 = this.transform.position;
oldQuaternion = this.transform.rotation;
}
if (Input.GetKeyDown(KeyCode.F2))
{
StartCoroutine(MovePoint());
}
}

private IEnumerator MovePoint()
{
//    oldVector3 = this.transform.position;
//   oldQuaternion = this.transform.rotation;
yield return new WaitForSeconds(0.2f);
newVector3 = this.transform.position;
newRotatinForward = this.transform.forward;

float lenth = Vector3.Distance(oldVector3, newVector3);
Vector3 velocity = Quaternion.Inverse(oldQuaternion) * newRotatinForward;
float angle = Mathf.Atan2(velocity.x, velocity.z) * 180.0f / 3.14159f;
Debug.Log(angle);//新的方向相对旧的方向的偏转角度
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: