Unity 相机跟随(向量的加减乘除)

329 阅读1分钟

列:运用向量知识完成相机一直跟随玩家并且看向该玩家(简版):

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

public class Camera_yidong : MonoBehaviour
{
    public Transform Player;//获取跟随物体的坐标值
    public float X_axle;//X轴的偏移量
    public float y_axle;//Y轴的偏移量
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    private void LateUpdate()
    {
        //摄像机的位置等于目标位置的向量偏移
        //先朝对象的面朝向方向移动X_axle米,再朝对象的头顶位置移动y_axle米;
        this.transform.position = Player.position + Player.forward * X_axle + Player.up * y_axle;
        this.transform.LookAt(Player);//确保相机视角对着目标物体
    }
}