# [SOS] Temple Run - How to Clamp X or Z?

Hi, i want to make a game namely as temple run

i want to move my player to left or right depend on the acceleration.
However, when i rotate my player, the horizontal moving axis is changed.
I have no idea how to solve it.

i am using

``````        transform.Translate(transform.forward * runSpd * Time.deltaTime, Space.World);
``````

to make my player run. However, transform.forward will change when i am rotated my object.

i want to make sth like new Vector 3(transform.forward.x * acceler.x, 0, transform.forward.z)
since transform forward will change if player is rotated, thus i don’t know how to do it.

i don’t know if i express my problem clearly… but hope you guys could feel my problem with my picture.

NEW QUESTIONS:

thx for #2. now i have another questions.
how to detect when should i clamp x or z? (player position)? or are there anyway to limit the player? i try to use rigidbody and collider, but it keep bouncing around…

Just do the same way you move them forward for moving left and right by translating along their right.

``````float lateralSpeed = 0.0f;

if(Input.GetKey(KeyCode.Left) == true) // or however you decide they're moving left
lateralSpeed = -10.0f;
if(Input.GetKey(KeyCode.Right) == true) // or however you decide they're moving right
lateralSpeed = 10.0f;

transform.Translate(transform.right * lateralSpeed * Time.deltaTime, Space.World);
``````
1 Like

I never thought that there are simple way like this. ty verymuch

how to detect when should i clamp x or z? (player position)

push

push

push

push

Did you got the soluation?

It’s shame your first two posts were to necro a 7 year old thread in such a pointless way.