在游戏中我们总是离不开人物对摄像机的跟随。
下面是摄像机跟随的几种方法。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class positionfollowing : MonoBehaviour
{
Vector3 offut;
public GameObject Tagat;
public float hight;
public float forword;
public float PositionSpeed;
void Start()
{
offut = Tagat.transform.position - transform.position;
}
void Update()
{
Camera.main.transform.position = transform.position - offut;
Camera.main.transform.position = Vector3.Lerp(transform.position, Tagat.transform.position, 0.3f);
//相机在人物后背多高多远
Vector3 v3 = Tagat.transform.position + hight * Tagat.transform.up - forword * Tagat.transform.forward;
transform.position = Vector3.Lerp(transform.position, v3, Time.deltaTime * PositionSpeed);
//跟lookat一样,只是缓慢差值旋转
Quaternion qu = Quaternion.LookRotation(Tagat.transform.position - v3);
transform.rotation = Quaternion.Slerp(transform.rotation, qu, Time.deltaTime * PositionSpeed);
}
}
运行调试位置
不足之处请多多包含。还请多多指教。