第一:线性
lerp:lerp是线性的速度变化,比如:float min=10;float max=20;
void Update{
transform.position = new Vector3(Mathf.Lerp(minimum, maximum, Time.time), 0, 0);注意此处的t要加时间Time.time.这样才能均匀缓动。
Lerp Angle:
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public float minAngle = 0.0F;
public float maxAngle = 90.0F;
void Update() {
float angle = Mathf.LerpAngle(minAngle, maxAngle, Time.time);
transform.eulerAngles = new Vector3(0, angle, 0);
}
}
// Fades from minimum to maximum in one second
//在1秒内从minimum渐变到maximum
var minAngle = 0.0;
var maxAngle = 90.0;
function Update () {
var angle : float = Mathf.LerpAngle(minAngle, maxAngle, Time.time);
transform.eulerAngles = Vector3
static function MoveTowards (current : float, target : float, maxDelta : float) : float
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
static function MoveTowardsAngle (current : float, target : float, maxDelta : float) : float
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public float target = 270.0F;
public float speed = 45.0F;
void Update() {
float angle = Mathf.MoveTowardsAngle(transform.eulerAngles.y, target, speed * Time.deltaTime);
transform.eulerAngles = new Vector3(0, angle, 0);
}
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public float target = 270.0F;
public float speed = 45.0F;
void Update() {
float angle = Mathf.MoveTowardsAngle(transform.eulerAngles.y, target, speed * Time.deltaTime);
transform.eulerAngles = new Vector3(0, angle, 0);
}
第三个:抛物线运动:
static function SmoothDamp (current : float, target : float, ref currentVelocity : float, smoothTime : float, maxSpeed : float = Mathf.Infinity, deltaTime : float = Time.deltaTime) : float
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Transform target;
public float smoothTime = 0.3F;
private float yVelocity = 0.0F;
void Update() {
float newPosition = Mathf.SmoothDamp(transform.position.y, target.position.y, ref yVelocity, smoothTime);
transform.position = new Vector3(transform.position.x, newPosition, transform.position.z);
}
static function SmoothDampAngle (current : float, target : float, ref currentVelocity : float, smoothTime : float, maxSpeed : float = Mathf.Infinity, deltaTime : float = Time.deltaTime) : float
这个值通过一些弹簧减震器类似的功能被平滑。这个函数可以用来平滑任何一种值,位置,颜色,标量。最常见的是平滑一个跟随摄像机。
public class example : MonoBehaviour {
public Transform target;
public float smooth = 0.3F;
public float distance = 5.0F;
private float yVelocity = 0.0F;
void Update() {
float yAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y, target.eulerAngles.y, ref yVelocity, smooth);
Vector3 position = target.position;
position += Quaternion.Euler(0, yAngle, 0) * new Vector3(0, 0, -distance);
transform.position = position;
transform.LookAt(target);
}