x


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;
    }
}
more ▼

asked Feb 20 '11 at 01:29 AM

SosaZero gravatar image

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
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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 gravatar image

Uriel_96
930 61 70 81

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

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