摄像机跟随之Vector3.Lerp(start,end,t)方法

来源:互联网 发布:世界史专业就业知乎 编辑:程序博客网 时间:2024/06/14 17:08

插值是数学上的一个概念,在这里用公式表示就是:from + (to - from) * t;这也就是Lerp的返回值(用这个公式分别算出x,y,z)。

static function Lerp (from : Vector3, to : Vector3, t : float) : Vector3 

from 是起始的位置,to是目标位置,按照数字t在from到to之间插值

 

using UnityEngine;
using System.Collections;

public class ThirdPersonCam : MonoBehaviour
{
    //摄像机所跟随的对象
    public Transform follow;
    //摄像机在水平方向与对象的距离
    public float distanceAway;        
    //摄像机在垂直方向与对象的距离
    public float distanceUp;
    //过渡速度
    public float smooth;                
    //摄像机的目标速度
    private Vector3 targetPosition;        
    
    //在LateUpdate中执行摄像机操作,确保在对象的操作完成之后
    void LateUpdate ()
    {
        //计算目标位置
        targetPosition = follow.position + Vector3.up * distanceUp - follow.forward * distanceAway;
        //对当前位置进行插值计算
    /    transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smooth);
        //使摄像机观察对象
        transform.LookAt(follow);
    }
}

 

0 0
原创粉丝点击