使目标朝向摄像机,随着摄像机转动

经过学习好几位大佬的文章,写出了这三行代码

挂在目标物体上就行,可以自己识别到主摄像机

功能为可控正反方向、可以已固定轴转动,目前视y轴跟随摄像机。

控制正负可以如第一行代码一样*-1,此代码目前顶视的情况下好像不要好,其他的问题就看实践中发现吧

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

public class LookAtview : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    private void Update()
    {
        //看向的位置 可以控制正负
        this.transform.LookAt(Camera.main.transform.position*-1);//控制正负
        //相对位置
        this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(Camera.main.transform.position - this.transform.position), 0);
        //确定变化的轴
        this.transform.rotation = new Quaternion(0, this.transform.rotation.y, 0, this.transform.rotation.w);
    }
}

感谢那些分享学识的大佬,非常感谢!我也分享一下自己的拙见。

猜你喜欢

转载自blog.csdn.net/weixin_64625272/article/details/122306533