1.Mathf.Clamp
为了限制某一物体的移动不超过一定的范围,可以用Mathf.Clamp来解决。
Mathf.Clamp(float value,float min,float max)
在Mathf.Clamp中传入三个参数:value,min,max
限制value的值在min,max之间,如果value大于max,则返回max
2.Mathf.Sign
static function Sign (f : float) : float
Description描述
返回 f 的符号。当 f 为正或为0返回1,为负返回-1。
3.Mathf.FloorToInt
static function FloorToInt (f : float) : int
Description描述
Returns the largest integer smaller to or equal to f.
返回最大的整数,小于或等于f。
// Prints 10
Debug.Log(Mathf.FloorToInt(10.0));
// Prints 10
Debug.Log(Mathf.FloorToInt(10.2));
// Prints 10
Debug.Log(Mathf.FloorToInt(10.7));
// Prints -10
Debug.Log(Mathf.FloorToInt(-10.0));
// Prints -11
Debug.Log(Mathf.FloorToInt(-10.2));
// Prints -11
Debug.Log(Mathf.FloorToInt(-10.7));
4.Mathf.CeilToInt
static function CeilToInt (f : float) : int
Description描述
Returns the smallest integer greater to or equal to f.
返回最小的整数大于或等于f。
// Prints 10
Debug.Log(Mathf.CeilToInt(10.0));
// Prints 11
Debug.Log(Mathf.CeilToInt(10.2));
// Prints 11
Debug.Log(Mathf.CeilToInt(10.7));
// Prints -10
Debug.Log(Mathf.CeilToInt(-10.0));
// Prints -10
Debug.Log(Mathf.CeilToInt(-10.2));
// Prints -10
Debug.Log(Mathf.CeilToInt(-10.7));
5.Mathf.Floor
static function Floor (f : float) : float
Description描述
Returns the largest integer smaller to or equal to f.
返回参数 f 中指定的数字或表达式的下限值。下限值是小于等于指定数字或表达式的最接近的整数。
// Prints 10
Debug.Log(Mathf.Floor(10.0));
// Prints 10
Debug.Log(Mathf.Floor(10.2));
// Prints 10
Debug.Log(Mathf.Floor(10.7));
// Prints -10
Debug.Log(Mathf.Floor(-10.0));
// Prints -11
Debug.Log(Mathf.Floor(-10.2));
// Prints -11
Debug.Log(Mathf.Floor(-10.7));
6.Mathf.Ceil
Mathf.Ceil上限值
static function Ceil (f : float) : float
返回 f 指定数字或表达式的上限值。数字的上限值是大于等于该数字的最接近的整数。
7.Mathf.RoundToInt
Mathf.RoundToInt四舍五入到整数
static function RoundToInt (f : float) :int
返回 f 指定的值四舍五入到最近的整数。
如果数字末尾是.5,因此它是在两个整数中间,不管是偶数或是奇数,将返回偶数。
8.Mathf.Infinity
表示正无穷,也就是无穷大。
9.Mathf.Lerp
static float Lerp(float from, float to, float t);
基于浮点数t返回a到b之间的插值,t限制在0~1之间。
当t = 0返回from,当t = 1 返回to。当t = 0.5 返回from和to的平均值。
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public float minimum = 10.0F;
public float maximum = 20.0F;
void Update()
{
transform.position = new Vector3(Mathf.Lerp(minimum, maximum, Time.time), 0, 0);
}
}