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.
Answer by streeetwalker · Apr 07, 2020 at 05:28 AM
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!
Answer by mcunha98 · Apr 07, 2020 at 12:21 PM
@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) )
@mcunha98, OK, that helps, but still not clear enough.
Is the distance from the origin you want to limit (pick one or describe another option):
1. the motion of an object along the circumference of the parabola?
2. how far from the origin the parabola hits the ground?
3. you want to move an object to the point where the parabola hits the ground?
In the example, I must to limit for example the distance in 9f. 9,094467>9
In code that you suggest, probably the problem is here:
parabola.endPosition.localPosition = (parabola.endPosition.localPosition + moveDist);
Because the endPosition continue to advanced beyond the limit of 9f
Now works fine !!!
Vector3 moveDist = parabola.endPosition.localPosition - parabola.startPosition.localPosition; if (moveDist.magnitude > parabola.maxDistance) { moveDist = moveDist.normalized * parabola.maxDistance; parabola.endPosition.localPosition = moveDist; }