Translate problem

I have follwing lines to translate the object.

 x=Input.GetAxis("Winch");

if ( (this.transform.localPosition.x >= -0.846) (this.transform.localPosition.x <= 0.818))

{
transform.Translate(x*0.01,0,0);
}

Here it works fine when we press positive button it goes forward and on negative button press it comes back.
But my que is when it reaches to one of the limit specified in script either positve or negative, it should translate/move automatically in opposite direction regardless of postive negative button press.
Does any one have idea?
please reply.
Thanks very much!

If I understand you correctly, what you want is that if, say, the ‘left’ key is being held down and the object reaches the limit on the left side, it ‘bounces’ and starts moving right, even though the ‘left’ key is still held down. In order to move left again, the user would then release the ‘left’ key and press it again.

If that’s what you’re looking for, then the logic (I think) will go something like this.

First, you’ll need to adjust your input code so that you can detect left and right ‘start’ and ‘stop’ events individually. You could do this by using e.g. GetButtonDown() and GetButtonUp(), or by adding some code to keep track of the previous state of the input axis.

Once you have this in place, the logic would be something like the following (pseudocode):

int direction = 0; // -1 for left, +1 for right, 0 for no motion

// In your update function:
if (left_button_pressed) { direction = -1; }
if (right_button_pressed) { direction = 1; }
if (left_button_released || right_button_released) { direction = 0; }

transform.Translate((float)direction * speed * Time.deltaTime);
if (transform.position.x < left_limit) {
    // May have to do this line differently, depending on language
    transform.position.x = left_limit;
    direction = 1;
} else if (transform.position.x > right_limit) {
    transform.position.x = right_limit;
    direction = -1;
}

Can’t guarantee I got all the details right, but that should be the general idea.

Sorry for the confusion. I was unable to clear an idea.
when I press left button object moves towords left side upto the limit 0.818 and when I press right key it reaches upto the the limit of -0.846 using this code.It is correct and as per my need.
But I also want scenerio like, when object reaches to the 0.818 by pressing left key as it reaches to that limit , it should return back (towards opposite direction i.e right ) automatically even if I press left key.
Alos when it reached to the right limit -0.846 it should retunr back towaords left regardless of button press.
Did you get what I mean?

I think Jesse already said everything, the algorithm seems good to me (although I would rather have used a function with a while loop until the player release the key, if possible. I don’t like using Update for loop, it is sometimes a hassle). :wink:

That’s what the code is intended to do. Is it not working?

The code is untested and written off the top of my head, so I can’t guarantee its correctness. However, the logic seems correct to me. Can you post your implementation of the algorithm? It may just be that in adapting the code you changed something that’s causing it not to work as expected.