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);
	}
}
var target = 20.0;
var speed = 5.0;

function Update() {
	transform.position = Vector3(Mathf.MoveTowards
	(transform.position.x, target, speed * Time.deltaTime), 0, 0);
}
最后修改:2011年2月23日 Wednesday 14:20

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。