Unity 摄像机跟随

时间:2021-01-16 01:49:14

方式一:将摄像机直接拖到游戏对象的下面;

方式二:脚本实现

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class kow : MonoBehaviour
{
public Transform targetTr;
public float dist = 10.0F;
public float height = 3.0F;
public float dampTrace = 20.0F; private Transform tr; // Start is called before the first frame update
void Start()
{
tr = GetComponent<Transform>();
} // Update is called once per frame
void LateUpdate()
{
tr.position = Vector3.Lerp(tr.position, targetTr.position - (targetTr.forward * dist) + (Vector3.up * height), Time.deltaTime * dampTrace);
tr.LookAt(targetTr.position);
}
}

这里主要的是Vector3.Lerp()和函数,三个参数,第一个:起始位置;第二个:结束位置;第三个:浮点格式。