- Home /
How do i Clamp the Z position of a rigidbody?
: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;
}
}
2 things I can just see are wrong, if(Input.GetButtonDown("D")){ will doesn't work the correct form is if(Input.Get$$anonymous$$eyDown("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.
Well actually if he created an input axis that is called "D" it will work. Agree with you on everything else.
yeah but I think he wants to use the a and d keys to move right and left.
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!
Answer by Uriel_96 · Feb 20, 2011 at 01:45 AM
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;
}
}