unityUI以Y轴朝向摄像头等

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LookAtCam : MonoBehaviour
{
    Transform mainCamTrans;
    void Start()
    {
        mainCamTrans = Camera.main.transform; //主摄像头
    }

    // Update is called once per frame
    void Update()
    {
        //Y轴朝向
        if(gameObject.activeSelf)
        {
            float x = transform.position.x - mainCamTrans.position.x;
            float z = transform.position.z - mainCamTrans.position.z;

            Vector3 dis = new Vector3(x,0,z);
            Quaternion lookAtRotation = Quaternion.LookRotation(dis, Vector3.up);
            this.transform.rotation = Quaternion.Lerp(this.transform.rotation, lookAtRotation, Time.deltaTime);

        }
    }
}
 

你可能感兴趣的:(编程,unity,1024程序员节)