Unity 旋转主要接口

2019-11-11  本文已影响0人  86a262e62b0b

一.Transform:

  1. eulerAngles:欧拉角
  1. LookAt:看向某一方向
  1. public void Rotate(Vector3 eulers, Space relativeTo = Space.Self):
  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):
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
 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);
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);
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
  1. public static Quaternion AngleAxis(float angle, Vector3 axis);
  1. Quaternion.Angle
public class ExampleClass : MonoBehaviour
{
    public Transform target;
    void Update()
    {
        float angle = Quaternion.Angle(transform.rotation, target.rotation);
    }
}
上一篇下一篇

猜你喜欢

热点阅读