Unity math function mathf

The mathf library is very powerful and basically encapsulates the functions of game mathematics.

First introduce a Mathf.pingpong

official api

Mathf .PingPong

static function PingPong (t : float, length : float) : float

DescriptionDescription _

PingPongs the value t, so that it is never larger than length and never smaller than 0.

Let the value t round-trip between 0 and length. The value of t is never greater than the value of length, and never less than 0.

The returned value will move back and forth between 0 and length.

The return value will move back and forth between 0 and 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);
} 
As the name suggests, it is like playing table tennis, back and forth, the first parameter is a T value, this value can be added all the time, for example time.time is the time in the game, it starts from 0 and keeps adding, so do some simple things When operating, you don't need to set the value yourself. Its practice method is to use if to judge, two values, one is the current T, and the other is the maximum value. If it is greater than the maximum value, it will continue to decrease. If it is less than the minimum value The value (0) is always added.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326305892&siteId=291194637