【 unity3d 】Vector3.Lerp()方法的理解

1:lerp方法声明,传入两个向量对象,第三个参数为间断值(几分之几,范围是0-1),返回一个计算后得到向量

2:具体计算得到返回值也就是 a + (b - a) * t

Vector3是个结构体类型,他们计算方式是分别根据 x , y , z进行相应计算的 。

这里列出Vector3的运算符重载



3:每次返回的值都是从a到b的某段距离

如:t = 0.1 也就是返回其a到b距离中的10分之1

可以在 update LateUpdate FixUpdate 方法里使用,造成拖拉移动缓动的效果

4:例子:

相机跟随玩家物体移动

脚本放在相机物体里

using UnityEngine;
using System.Collections;

public class CameraCs : MonoBehaviour {

	public float _DposZ = 4f;
	public float _DposY = 6f;
	public float _Dumping = 5f;
	Transform target;

	void Start () {
		if (target == null)
			target = GameObject.Find ("Yellowor2").transform;
	}

	void Update () {
	
	}

	void LateUpdate(){
		Vector3 newPosition = target.position - target.forward * _DposZ + new Vector3 (0, _DposY, 0);
		//防止卡顿所以要*deltaTime移动
		transform.position = Vector3.Lerp (transform.position, newPosition,_Dumping*Time.deltaTime );
		//为了防止相机抖动,放在设置位置的后边。
		transform.LookAt (target);	
	}
}

猜你喜欢

转载自blog.csdn.net/liaoshengg/article/details/80751472