# How do i Clamp the Z position of a rigidbody?

 0 :D What i have doesnt exactly Clamp =/ here's my code: ``````if(Input.GetButtonDown("A")) { transform.position.z += 1.7; if(transform.position.z == 83.75) { transform.position.z = 83.75; } } if(Input.GetButtonDown("D")) { transform.position.z -= 1.7; if(transform.position.z == 80.75) { transform.position.z = 80.75; } } `````` more ▼ asked Feb 20 '11 at 01:29 AM SosaZero 5 ● 2 ● 2 ● 7 2 things I can just see are wrong, if(Input.GetButtonDown("D")){ will doesn't work the correct form is if(Input.GetKeyDown("D")){, then the second one iif(transform.position.z == 80.75)transform.position.z = 80.75;???????????????????????????????? you are saying that if the position of the transform is = 80.75 the position of the object it will be the same?????? don't you think that doesn't make sense. Feb 20 '11 at 01:41 AM Uriel_96 Well actually if he created an input axis that is called "D" it will work. Agree with you on everything else. Feb 20 '11 at 05:01 AM Bunny83 yeah but I think he wants to use the a and d keys to move right and left. Feb 20 '11 at 02:59 PM Uriel_96 about what uriel said:"you are saying that if the position of the transform is = 80.75 the position of the object it will be the same?????? don't you think that doesn't make sense."so i have to write this?: if(transform.position.z >= 80.75) { transform.position.z = 80.75; }that worked! :) thnx guys! Feb 20 '11 at 05:34 PM SosaZero add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 my suggestion for your script: ``````if(Input.GetKeyDown("a")) { transform.position.z += 1.7; if(transform.position.z >= 83.75) { transform.position.z = 83.75; } } if(Input.GetKeyDown("d")) { transform.position.z -= 1.7; if(transform.position.z <= 80.75) { transform.position.z = 80.75; } } `````` more ▼ answered Feb 20 '11 at 01:45 AM Uriel_96 930 ● 61 ● 70 ● 81 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x3722
x1783
x884
x453
x80

asked: Feb 20 '11 at 01:29 AM

Seen: 1025 times

Last Updated: Feb 20 '11 at 01:29 AM