Hello, everyone. I’m trying to toss an object so that it always lands at a determined point, regardless of where it starts initially, and always in the same amount of time. I am trying to use basic kinematics to achieve this, but the object is not reaching the point as I desire. Here is my code:
float d = Vector3.Distance(destination,rb.transform.position); //Get distance between the current position and the destination
float tsqr = timeToReach*timeToReach; //Get the time to reach the destination squared
float m = rb.mass; //Get the mass of the object to be tossed
float f = (float)(d/((.5)*tsqr))*m; //d = v*t + (1/2)at^2 substituting a for f/m and solving for f
rb.AddForce (f * Vector3.forward); //Apply force to rigidbody
What might I be doing wrong? Any help would be greatly appreciated.
Supposing the equation is correct for what you want to do, you should apply a constant force to get the desired result - a single AddForce applies the force only during one physics cycle, which usually is 20mS (unless you change Fixed Timestep in the Time Manager). You should instead use AddForce in FixedUpdate to apply the constant force, or add a Constant Force component and set its force property to the calculated value.
But I suspect that this is not what you actually want: applying the calculated constant force accelerates an object from 0 to some velocity in such a way that the object crosses the specified distance in the desired time - but only if gravity is off: if gravity is on, the rigidbody also falls towards -Y (default gravity), crashing to the ground or reaching a point well below your target. If gravity is on, you must use a ballistic equation instead - but solving it to reach the target in a predefined time is a pain in the ass.
I created a function that calculates the initial direction/velocity to throw an object so that it lands at the target position, provided that the starting and target positions are approximately at the same heights (the function compensates for height differences that are small when compared to the horizontal distance). The time to reach the target can’t be specified, but if this can help you, take a look at my answer in this question.