Restricting movement along an axis

Veyr new to Unity, and trying to get the ball rolling.

Follows a few tutorials, and now i’m stuck as id’ like to get my player to be restricted within a boundary on the axis.

this is my player movement code…

amtToMove = playerSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;

transform.Translate(Vector3.right * amtToMove);

However I haven’t a clue where to go from here.

Any help, redirects would be most appreciated

This is the prefab i instantiate: https://gyazo.com/e1b7ab7a95f9a44098df182a1bd5c37d And this is the object i try to change values on: https://gyazo.com/bfc6136f3a3c98bf8f64c0d6a996a8df

As Commoble said. Either it's not finding that gameobject named "Name" or the gameobject doesn't have Text. OR Druk_A_C[A] isn't returning an actual object.

2 Answers

2

If you want to limit the X range within which the player is allowed to move, take a look at the position element of the player’s transform, and use Mathf.Clamp() to restrict it to that range.

Maybe provide more data on the problem. (my first post/answer :-) ) Maybe include a sort of counter with a limit like if(amtToMove>7) amtToMove=7; There is an extension called itween(free in unity store) that will animate things along a spline line which you can see in the editor. Or if you want to be able to move sideways along the path and the level is not so long maybe line the sides of the path with box colliders all the way >:)

i googled “Mathf.Clamp”

and i got some example code, tweeked and it workied. Thank you!

Please stop posting comments as answers.