美文网首页
Unity 旋转主要接口

Unity 旋转主要接口

作者: 86a262e62b0b | 来源:发表于2019-11-11 15:26 被阅读0次

    一.Transform:

    1. eulerAngles:欧拉角
    • x,y和z角度分别表示围绕z轴旋转z度,围绕x轴旋转x度和围绕y轴旋转y度。
    • 不要增加它们,因为当角度超过360度时,它将失败。请改用Transform.Rotate。
    • 实践后没有实现
    1. LookAt:看向某一方向
    1. public void Rotate(Vector3 eulers, Space relativeTo = Space.Self):
    • 放在Update下面,持续旋转
    • 度数旋转
    • 欧拉旋转
    1. public void RotateAroundVector3 point, Vector3 axis,float angle):
    • 绕某点的某轴旋转,比如行星环绕
    1. rotation:存储一个四元数。您可以使用它来旋转GameObject或提供当前旋转。请勿尝试编辑/修改旋转。Transform.rotation小于180度。

    二. Quaternion

    1. public static Quaternion LookRotation(Vector3 forward, Vector3 upwards = Vector3.up):
    • Z轴对齐forward,X轴对齐,forward和upwards的叉积
    public Transform target;
    void Update()
    {
        Vector3 relativePos = target.position - transform.position;
        // the second argument, upwards, defaults to Vector3.up
        Quaternion rotation = Quaternion.LookRotation(relativePos, Vector3.up);
        transform.rotation = rotation;
    }
    
    1. Quaternion.Euler
    • 围绕轴旋转n角度
     void Start()
    {
            // A rotation 30 degrees around the y-axis
            Quaternion rotation = Quaternion.Euler(0, 30, 0);
    }
    
    1. public static Quaternion Slerp(Quaternion a, Quaternion b, float t);
    • 在a和b之间进行球面插值。限制参数t在[0,1]范围内。
    public class ExampleClass : MonoBehaviour
    {
        public Transform from;
        public Transform to;
    
        private float timeCount = 0.0f;
    
        void Update()
        {
            transform.rotation = Quaternion.Slerp(from.rotation, to.rotation, timeCount);
            timeCount = timeCount + Time.deltaTime;
        }
    }
    
    1. public static Quaternion FromToRotation(Vector3 fromDirection, Vector3 toDirection);
    • 创建从旋转fromDirection到的旋转toDirection。
    • 通常,您可以使用它来旋转变换轴.
    void Start()
    {
            // Sets the rotation so that the transform's y-axis goes along the z-axis
            transform.rotation = Quaternion.FromToRotation(Vector3.up, transform.forward);
    }
    
    1. Quaternion.identity
    • 只读
    1. Quaternion.RotateTowards
    • from四元数旋转朝向to通过的角步骤maxDegreesDelta
    1. public static Quaternion AngleAxis(float angle, Vector3 axis);
    • 围绕axis轴旋转angle
    1. Quaternion.Angle
    • ?
    public class ExampleClass : MonoBehaviour
    {
        public Transform target;
        void Update()
        {
            float angle = Quaternion.Angle(transform.rotation, target.rotation);
        }
    }
    

    相关文章

      网友评论

          本文标题:Unity 旋转主要接口

          本文链接:https://www.haomeiwen.com/subject/fhwgbctx.html