Mathf.MoveTowards 移向
static function MoveTowards (current : float, target : float, maxDelta : float) : float
Description描述:
Moves a value current towards target.
改变一个当前值向目标值靠近。
This is essentially the same as Mathf.Lerp but instead the function will ensure that the speed never exceeds maxDelta. Negative values of maxDelta pushes the value away from target.
这实际上和 Mathf.Lerp相同,而是该函数将确保我们的速度不会超过maxDelta。maxDelta为负值将目标从推离。
using UnityEngine; using System.Collections; public class example : MonoBehaviour { public float target = 20.0F; public float speed = 5.0F; void Update() { transform.position = new Vector3(Mathf.MoveTowards(transform.position.x, target, speed * Time.deltaTime), 0, 0); } }