- Home /
Question by
prateekkp · Jul 30, 2014 at 09:52 AM ·
projectileslingshotparabolic
Calculate parabolic initial velocity and angle?
Hi, I need to find the parabolic initial velocity and angle of projection in 2D. Given: initial point, final point, range, height, delta height.
As show in the figure the final point can be below or above the initial point. I have tried Itween Accurate Lob but does not give accurate projectile path in the end. Please help anyone?
I am using the following code rigth now but it does not include height parameter.
void Update() {
rigidbody2D.velocity = JumpVelocity (Camera.main.ScreenToWorldPoint (Input.mousePosition), angleInput); }
Vector2 JumpVelocity (Vector3 targetPosition, float angle) { var distanceVector = -transform.position + targetPosition; float height = distanceVector.y; distanceVector.y = 0f;
var distanceScalar = distanceVector.magnitude;
var angleInRadian = angle * Mathf.Deg2Rad;
distanceVector.y = distanceScalar * Mathf.Tan (angle);
distanceScalar += height / Mathf.Tan (angleInRadian);
float sqr = distanceScalar * (Physics.gravity.magnitude * 5) / Mathf.Sin (2 * angleInRadian);
sqr = sqr < 0 ? -1 * sqr : sqr;
var velocity = Mathf.Sqrt (sqr) * 100f;
return velocity * distanceVector.normalized;
}
ask.png
(38.4 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613155352im_/https://answers.unity.com/themes/thub/images/avi.jpg)