Unity3D中Mathf数学运算函数总结积累

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);
      }
 }
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值