Unity朝向某个物体(星体围绕太阳旋转)

方式一:

public Transform target;

void Update () { 
        transform.LookAt(target);
    }

方式二:

    public Transform target;
    void Update () {
        Vector3 relativePos = target.position - transform.position;
        transform.rotation = Quaternion.LookRotation(relativePos);
    }

例子:星体围绕太阳旋转:

    public Transform target;
    void Update () 
    {
        Vector3 relativePos = (target.position + new Vector3(0, 1.5f, 0)) - transform.position;
        Quaternion rotation = Quaternion.LookRotation(relativePos);
        Quaternion current = transform.localRotation;
        transform.localRotation = Quaternion.Slerp(current, rotation, Time.deltaTime);
        transform.Translate(0, 0, 3 * Time.deltaTime);
    }

 

你可能感兴趣的:(unity,Transform)