使用Vector3.Lerp实现Camera(摄像头)平滑跟随物体移动

首先介绍一下Vector3.Lerp(Vector a, Vector b, float t)方法,这是一个向量插值方法,t的取值范围是0-1,t向0靠近,返回值向a靠近;t向1靠近,返回值向b靠近。
下面对Lerp的返回值和t的值关系进行测试
测试代码:

    Vector3 v1 = new Vector3(0, 0, 0);
    Vector3 v2 = new Vector3(10, 10, 10);
    Debug.Log(Vector3.Lerp(v1, v2, smooth));

测试过程:
smooth = -10 —> 输出:(0.0, 0.0, 0.0)
smooth = 0 —> 输出: (0.0, 0.0, 0.0)
smooth = 0.1 —> 输出:(1.0, 1.0, 1.0)
smooth = 0.2 —> 输出: (2.0, 2.0, 2.0)

smooth = 0.9 —> 输出: (9.0, 9.0, 9.0)
smooth = 1 —> 输出:(10.0, 10.0, 10.0)
smooth = 10 —> 输出: (10.0, 10.0, 10.0)

总结:
当0 <= t <= 1时,Lerp返回值为:a * (1-t) + b * t
当t > 1时,Lerp的返回值为: b
当t < 0时,Lerp的返回值为: a

实现摄像头跟随物体移动:

using UnityEngine;
using System.Collections;

public class CameraFollow : MonoBehaviour {

    public Transform target;    //camera的跟随目标
    public float smooth = 0.5f; //平滑量
    Vector3 distance;

    void Start()
    {
        distance = transform.position - target.position; //target到camera的距离
    }

    void Update()
    {
        transform.position = Vector3.Lerp(transform.position, target.position + distance, smooth);

    }
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值