# How limit the movement using distance

How can I limit the distance of my object ?
The code work, but the position always is restarted to zero.

``````            horizontal = horizontal * (movimento * Time.deltaTime);
vertical = vertical * (movimento * Time.deltaTime);
Vector3 fwd = new Vector3(horizontal * -1f, 0f, vertical * -1f);
if (fwd != Vector3.zero)
{
parabola.endPosition.forward = fwd;
}

Vector3 rot = new Vector3(horizontal, 0f, vertical);
if (rot != Vector3.zero)
{
parabola.endPosition.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(rot), 10f * Time.deltaTime);
parabola.endPosition.Translate(horizontal * -10f, 0f, vertical * -10f, Space.World);
}

float dist = Vector3.Distance(parabola.startPosition.localPosition, parabola.endPosition.localPosition);
if (dist > parabola.maxDistance)
{
Vector3 newpos = parabola.endPosition.localPosition - parabola.startPosition.localPosition;
newpos = newpos.normalized;
newpos *= (dist - parabola.maxDistance);
parabola.endPosition.localPosition = newpos;
}
``````

When I change the position of endPosition using parabola.endPosition.localPosition = newpos; the code not work.

Hi @mcunha98, sorry, but I’m confused. the code you show us. It is not enough to tell what is going on, and you don’t explain it - what are you supposed to be moving? The end point of something is supposed to get closer and closer to some fixed point, or what?

To limit the movement of something, here is one way I would do:

``````Vector3 moveDist = parabola.endPosition.localPosition - parabola.startPosition.localPosition;
if ( moveDist.magnitude > parabola.maxDistance ) // moving too far?
moveDist  =  moveDist.normalized * parabola.maxDistance; // then limit it
object.transform.localPosition = object.transform.localPosition + moveDist;
``````

Now, if you want to do something else, let us know!

@streeetwalker I’ll do a simple test with you shared, but my goal is

I attach the parabola script object to my gameObject (player is the Origin) and a simple Empty is moved by cursor showing the trajectory of the parabola. Its works fine, but the object to finish the trajectory need a limit (max distance) from the origin .

So basically, after move the endPoint using input, I just need to check if I reach the maximum distance and stop to go forward (in my sample code, the step if (dist > parabola.maxDistance) )