- Home /
How to create parabola path for addforce()
I used Unity's addforce() to throw a ball into the air. But i want the player to know where his balls are going to. I tried using regular parabola formula to do it but it uses velocity and the addforce uses, well, force. Is there any other way to make a parabola or atleast a way to convert the regular formula to use force instead of velocity? Here's my code
Vector3 CalcArcPos(float t, float maxDis)
{
float x = t * maxDis;
float y = x * Mathf.Tan(arcRadian) - ((g * x * x) / (2 * arcVel * arcVel * Mathf.Cos(arcRadian) * Mathf.Cos(arcRadian)));
return new Vector3(x, y, canon.transform.position.z);
}
void CalcArc()
{
arcHolder.transform.localRotation = Quaternion.Euler(arcHolder.transform.localEulerAngles.x, arcHolder.transform.localEulerAngles.y, -canon.transform.localEulerAngles.x); ;
Vector3[] poses = new Vector3[arcImgAmt];
arcRadian = Mathf.Deg2Rad * angle;
float maxDis = (arcVel * arcVel * Mathf.Sin(2 * arcRadian)) / g;
for (int i = 0; i < arcImgAmt; i++)
{
float t = (float)i / arcImgAmt;
poses[i] = CalcArcPos(t, maxDis);
arcImgs[i].transform.localPosition = poses[i];
}
}
Thank you for reading this and answering it
Have you tried using the velocity change mode in your AddForce call? This is pretty much what it's there for.
That would definitely a simpler approach than trying to work out what velocity a force applied in another mode is going to result in (which is essentially what you're asking for, and involves not only additional variables such as mass and duration, but also means you're relying even more on the accuracy of the physics system)
Answer by Bunny83 · Oct 25, 2019 at 08:33 PM
Your question is missing an important detail. AddForce has 4 different modes and the result highly depends on how you use AddForce. I have explained the different force modes over here. Only one of these force modes is an actual force (ForceMode.Force). All the other modes either describe a change in velocity or energy.
Your forumla only works with the initial velocity. How and when you reach it also highly depends on how you use AddForce. If you actually applying a force every frame, it's important to know for how long you apply it. During the acceleration period it would be extremely difficult to tell the exact movement part ahead of time unless you know exactly how long you're going to apply the force. Though this would be the same as if you just calculate the velocity at the moment you stop applying the force.
If you just apply an "impulse" you essentially set the velocity directly to a certain value. This would be your initial velocity. Maybe you already use the wrong force mode for your specific case? We don't know since, as I mentioned, without knowing how you use AddForce this question is too ambiguous.
Answer by lgarczyn · Oct 25, 2019 at 11:12 PM
Unity doesn't actually uses linear integration to calculate paths. AddForce is simply a wrapper over an instant incrementation of the velocity.
To calculate the position after a time t, the best option is to simply iterate X times, incrementing your position by velocity * Time.fixedDeltaTime
, and your velocity by force * Time.fixedDeltaTime
, X being t / Time.fixedDeltaTime
.
Here is my previous answers on the subject: https://stackoverflow.com/questions/57996836/how-does-spaceflight-simulator-predict-flight-paths/57998714#57998714 and https://answers.unity.com/questions/1671525/calculate-a-trajectory-in-all-possible-directions.html?childToView=1671641#answer-1671641
Trying to use parabolas will only lead to errors and headaches.
Your answer
Follow this Question
Related Questions
C# script not working 1 Answer
Throwing a Spear 1 Answer
Getting launch angle for projectile given height, distance and speed in 3D 2 Answers
Multiple Cars not working 1 Answer
Fireing a tank projectile. 1 Answer