mathf这个库十分强大,基本上封装了 游戏数学方面的函数,
先介绍一个Mathf.pingpong
官方api
Mathf.PingPong 乒乓
static function PingPong (t : float, length : float) : float
Description描述
PingPongs the value t, so that it is never larger than length and never smaller than 0.
让数值t在 0到length之间往返。t值永远不会大于length的值,也永远不会小于0。
The returned value will move back and forth between 0 and length.
返回值将在0和length之间来回移动。
Update () {
// Set the x position to loop between 0 and 3
//设置x位置循环在0和3之间
transform.position = new Vector3(
Mathf.PingPong(Time.time, 3), transform.position.y, transform.position.z);
}
顾名思义,就是像打乒乓球 那样 ,来回, 第一个参数是一个T值,这个值可以是一直加的,比如time.time 就是游戏里的时间啦,从0开始一直加, 这样再做一些简单操作时,就不用自己去设定值了,
实现原理是 判断倍数奇偶,再求余,再运算。
如果当前的T除去最大L,倍数为双时,0246810,进行求模运算,%,
倍数单时,返回值R= L-(当前值T%最大值L),R=36 -(37%36) ,为35,进行减去运算,
注意:方法会返回最大值 0~L 中, 而第一个参数就是要进行判断的值,而不是最小值。 可以将变量设置成一个增量,
1~L float T+=time.delTime;
Mathf.clamps
Clamps a value between a minimum float and maximum float value.
限制value的值在min和max之间, 如果value小于min,返回min。 如果value大于max,返回max,否则返回value
这个函数比较常用, 在处理角色移动时,常常需要去限制他们。