有关unity3d中Mathf的用法。

有关unity3d中Mathf的用法。_第1张图片


第一:线性

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
曲线运动:先慢后慢:

Mathf.MoveTowards 移向

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
把本身的位置移动到目标点的位置。以什么速度。

Mathf.MoveTowardsAngle 移动角

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);
	}
第三个:抛物线运动:

Mathf.SmoothDamp 平滑阻尼

static function SmoothDamp (current : float, target : float, ref currentVelocity : float, smoothTime : float, maxSpeed : float = Mathf.Infinity, deltaTime : float = Time.deltaTime) : float 

  • current
    The current position.
    当前的位置。
  • target
    The position we are trying to reach.
    我们试图达到的位置。
  • currentVelocity
    The current velocity, this value is modified by the function every time you call it.
    当前速度,这个值在你访问这个函数的时候会被随时修改。
  • smoothTime
    Approximately the time it will take to reach the target. A smaller value will reach the target faster.
    要到达目标位置的近似时间,实际到达目标时要快一些。
  • maxSpeed
    Optionally allows you to clamp the maximum speed.
    可选参数,允许你限制的最大速度。
  • deltaTime
    The time since the last call to this function. By default Time.deltaTime.
    上次调用该函数到现在的时间。缺省为Time.deltaTime。 
这只是需要三个参数就能够正常运行。

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);
	}

Mathf.SmoothDampAngle 平滑阻尼角度

static function SmoothDampAngle (current : float, target : float, ref currentVelocity : float, smoothTime : float, maxSpeed : float = Mathf.Infinity, deltaTime : float = Time.deltaTime) : float 

  • current
    The current position.
    当前的位置。
  • target
    The position we are trying to reach.
    我们试图达到的位置。
  • currentVelocity
    The current velocity, this value is modified by the function every time you call it.
    当前速度,这个值在你访问这个函数的时候会被随时修改。
  • smoothTime
    Approximately the time it will take to reach the target. A smaller value will reach the target faster.
    要到达目标位置的近似时间,实际到达目标时要快一些。
  • maxSpeed
    Optionally allows you to clamp the maximum speed.
    可选参数,允许你限制的最大速度。
  • deltaTime
    The time since the last call to this function. By default Time.deltaTime.
    上次调用该函数到现在的时间。缺省为Time.deltaTime。 

这个值通过一些弹簧减震器类似的功能被平滑。这个函数可以用来平滑任何一种值,位置,颜色,标量。最常见的是平滑一个跟随摄像机。

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);
	}


你可能感兴趣的:(unity3d开发)