- Home /
Invert gameobject movement gradually when reaching a certain Vector3
I'm working on a 2D game and I have a script to move an object to a certain position with a constant speed using Vector3.MoveTowards
but now, I need to simulate gravity in order that it reverts the direction of the object when it reaches a certain "limit" on Z axis.
I could use a fixed value for gravity but the problem is that the speed of my object can be something between 10 and 20 when it starts moving...
Here's an image demonstrating what I want:
So, how can I do that? Is there any way to get the value of gravity to use in order that the object inverts on the given "limit" based on it's speed?
Answer by sparkzbarca · Dec 27, 2013 at 02:21 PM
so you want to make sure the objects fall mirrors its rise?
SOURCED:: http://forum.unity3d.com/threads/12588-Projection-Trajectory-Arc
function GetQuadraticCoordinates(t : float) : Vector3 {
return Mathf.Pow(1-t,2)*p0 + 2*t*(1-t)*c0 + Mathf.Pow(t,2)*p1 ;
}
where:
p0 - is the start point
p1 - is the end point
c0 - is the middle point ( controlling the arcness )
t - between 0 and 1
so use the equation
void update()
{
transform.position = vector3.slerp(currentPos, getquadcoordiantes(speed * time.deltatime), 1);
}
that should work I think
So, p0 is my bottom right point of the curved line in the image? But what about the other two? Which one is the value of "limit"? And I don't have the value of the bottom left point. I want it to be calculated automatically.
limit is the midpoint you calculate
you can yourself autocalculate the endpoint
endpoint is
int Xmod;
int Zmod;
if(startpoint.x > midpoint.x)
Xmod = startpoint.x - midpoint.x
else
Xmod = midpoint.x - startpoint.x
if(startpoint.z > midpoint.z)
Zmod = startpoint.z - midpoint.z
else
Zmod = midpoint.z - startpoint.z
that gets you the distance between the start and mid which is half way
now we double that to get the end pos
so
endposition.set(startposition.x + Xmod * 2, startposition.y, startposition.z + Zmod * 2);
that gets us the end.
this assumes you figured out the co-ordinates of the mid. I dont know what your wanting enough to know how you find that but yea i mean its the mid point you decide on, its the limit point.