【1.关于视角跟随主角移动】 2017.11.29
学习来源是:https://www.cnblogs.com/kerrysx/p/4750010.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour {
public Transform target;
//主角,也就是目标物体
public float smothing = 5f;
//镜头平滑移动的速度
Vector3 offset;
//摄像机和主角之间的固定距离
void Start () {
target = GameObject.FindGameObjectWithTag ("Player").transform;
offset = transform.position - target.position;
//摄像机和主角之间的固定距离
}
void FixedUpdate () {
Vector3 targetCampos = target.position + offset;
transform.position = Vector3.Lerp(transform.position, targetCampos, smothing * Time.deltaTime);
//Vector3.Lerp的作用就是以一定的速度比例t,平滑的从start位置移动到end位置
}
}
原理很简单,是一开始取镜头和主角的位置并通过相减得到固定的距离,然后镜头一直保持这个固定的距离,这样就可以实现镜头跟随。
关