How to Clamp X-axis Movement?

Hey guys,

I’m currently working on a game where I want the player to constantly move upwards (Y-Axis), I am having trouble trying to figure out how to clamp the X-axis only. Can anyone help me figure this problem out? At the moment, this is the script I wrote to stop the player from moving outside the boundaries:

rigidbody2D.position = new Vector2 
(Mathf.Clamp (rigidbody2D.position.x, GameAttributes.ScreenMinimumWidth, GameAttributes.ScreenMaximumWidth), 0);

Thank you guys for the help

  • Beeraj

The problem has now been fixed, thanks to Razacx - this was the solution:

Is the problem that the object can’t move on the y-axis in this script? In that case, instead of using 0 as the y value, use rigidbody2D.position.y