Limit the range of axial movement in unity mathf.clamp

Mathf.Clamp

in the game, in order to limit the movement of a certain axis of the player within a certain range, mathf.clamp can be used to solve the problem

Mathf.Clamp(float value,float min,float max)

Pass in three parameters in mathf. Clamp: value, min, max

Limit the value between min and max. if value is greater than max, Max is returned. If value is less than min, min is returned. Otherwise, value is returned

For example:

_ rig.transform.position = new Vector3(transform.position.x, transform.position.y,
Mathf.Clamp(_ rig.transform.position.z, -20.0f, 28.0f));

Here, the movement of the rigid body in the z-axis direction is limited, and the rigid body moves in the range of – 20.0 to 28.0

Similar Posts: