Unity API Mathf的使用

Mathf里面的静态常量

Deg2Rad

角度转为弧度(角度 * Mathf.Deg2Rad)

Rad2Deg

弧度转为角度(弧度 * Mathf.Rad2Deg)

Infinity

表示一个无限大的数字

NegativeInfinity

表示一个无限小的数字

PI

 

Epsilon

表示一个非常小的小数

print("Mathf.Deg2Rad:" + Mathf.Deg2Rad);//角度转为弧度(角度 * Mathf.Deg2Rad)
print("Mathf.Rad2Deg:" + Mathf.Rad2Deg);//弧度转为角度(弧度 * Mathf.Rad2Deg)
print("Mathf.Infinity:" + Mathf.Infinity);//表示一个无限大的数字
print("Mathf.NegativeInfinity:" + Mathf.NegativeInfinity);//表示一个无限小的数字
print("Mathf.PI:" + Mathf.PI);
print("Mathf.Epsilon:" + Mathf.Epsilon);//表示一个非常小的小数

 

Mathf中的Clamp限定方法

Abs

取绝对值

Ceil

向上取整

CeilToInt

向上取整返回int类型

Clamp

把一个值限定在一个范围之内

Clamp(float value, float min, float max)

如果value比min小,则返回min

如果value比max大,则返回max

如果value在min和max中间,则返回value

Clamp01

把一个值限定在0和1之间

Mathf中的常用方法

ClosestPowerOfTwo

取得离2的次方最近的数

DeltaAngle

计算两个角度之间的最短差值,取两个角度最小夹角。

Floor

向下取整

FloorToInt

向下取整返回int

Max

取最大值

Min

取最小值

Pow

Pow(float f, float p)

取f的p次方

Sqrt

取参数的平方根

Debug.Log(Mathf.Floor(10.0F));//10
Debug.Log(Mathf.Floor(10.2F));//10
Debug.Log(Mathf.Floor(10.7F));//10
Debug.Log(Mathf.Floor(-10.0F));//-10
Debug.Log(Mathf.Floor(-10.2F));//-11
Debug.Log(Mathf.Floor(-10.7F));//-11
// 2 4 8 16 32
print(Mathf.ClosestPowerOfTwo(2));//2
print(Mathf.ClosestPowerOfTwo(3));//4
print(Mathf.ClosestPowerOfTwo(4));//4
print(Mathf.ClosestPowerOfTwo(5));//4
print(Mathf.ClosestPowerOfTwo(6));//8
print(Mathf.ClosestPowerOfTwo(30));//32
print(Mathf.Max(1, 2));//2
print(Mathf.Max(1, 2, 5, 3, 10));//10
print(Mathf.Min(1, 2));//1
print(Mathf.Min(1, 2, 5, 3, 10));//1
print(Mathf.Pow(4, 3));//64 
print(Mathf.Sqrt(3));//1.73

使用Lerp插值运算做变速运动(先快后慢)

在一定范围内,按照给定的比例来往中间插入一个值。是按比例来运动,所以才会越来越慢

float Lerp(float a, float b, float t);

属性

a

起始值

b

结束值

t

0-1的比例

假设起始值为9,结束值为20,比例为0.5

public class API10Mathf : MonoBehaviour {
    public Transform cube;    
    void Start()
    {
        cube.position = new Vector3(0, 0, 0);
    }
    void Update()
    {
        float x = cube.position.x;
        float newX = Mathf.Lerp(x, 10, Time.deltaTime);//由x向目标位置移动一定的比例,会越来越慢        
        cube.position = new Vector3(newX, 0, 0);
    }
}

float LerpAngle(float a, float b, float t);

与Lerp相同,但是确保当它们环绕360度时内插正确。

参数t的取值范围为[0,1]。变量a和b的单位为度。

float angle = Mathf.LerpAngle(10, 250, Time.time);
cube.eulerAngles = new Vector3(0, angle, 0);

使用MoveTowards做匀速运动 

起始值按移动的间距进行移动

float MoveTowards(float current, float target, float maxDelta);

current

当前值

target

目标值

maxDelta

移动的间距

public class API10Mathf : MonoBehaviour {
    public Transform cube;
    public float speed = 3;
    void Start()
    {
        cube.position = new Vector3(0, 0, 0);
    }
    void Update()
    {
        float x = cube.position.x; 
        float newX = Mathf.MoveTowards(x, 10, Time.deltaTime * speed);
        cube.position = new Vector3(newX, 0, 0);
    }
}

使用PingPong方法实现乒乓的来回匀速运动效果

默认值是0,取值范围在0 – length之间

float PingPong(float t, float length);

length

最大值

t

运动速度,一直增大的数

public class API10Mathf : MonoBehaviour {
    public Transform cube;
    public float speed = 3;
    void Start()
    {
        cube.position = new Vector3(0, 0, 0);
    }
    void Update()
    {
       //在0 - 5来回运动
       cube.position = new Vector3( Mathf.PingPong(Time.time * speed, 5), 0, 0);

       //在5 - 10来回运动
       cube.position = new Vector3(5 + Mathf.PingPong(Time.time * speed, 5), 0, 0);
    }   
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值