Unity人员始终面对摄像机

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

public class FollowCamera : MonoBehaviour
{
    //主摄像机对象
    private Camera _camera;
    public bool isBack = false;

    void Start()
    {
        _camera = Camera.main;   
    }

    private void Update()
    {
        // 物体始终面向摄像机
        if (_camera == null) return;
        var rotation = Quaternion.LookRotation(_camera.transform.TransformVector(isBack?Vector3.back : Vector3.forward),
            _camera.transform.TransformVector(Vector3.up));
        rotation = new Quaternion(0, rotation.y, 0, rotation.w);
        gameObject.transform.rotation = rotation;
    }
}


posted @ 2021-12-20 08:48  Excel2016  阅读(232)  评论(0)    收藏  举报