Question by
zann06 · May 05, 2020 at 02:08 PM ·
c#shootingprojectiletrajectoryballistic
Projectile trajectory based on angle
In my 3D game (top down shooter), I try to do this: When you click the mouse button (raycast on point) on a specific area on the map -> a shot was fired from the gun -> the projectile flies in the direction of the barrel with a certain path and at the same distance as the mouse pointer (but not the direction). I already have the source code for calculating the projectile trajectory. Help make the projectile fly in the direction of the barrel, BUT with the distance shown by the mouseposition raycast, NOT the mouseposition direction.
private void FireCannonAtPoint(Vector3 point)
{
var velocity = BallisticVelocity(point, _angle);
GameObject projectile = Instantiate(_projectile, _shootPoint.transform.position, Quaternion.identity) as GameObject;
projectile.GetComponent<Rigidbody>().velocity = velocity;
}
private Vector3 BallisticVelocity(Vector3 destination, float angle)
{
Vector3 dir = destination - _shootPoint.transform.position;
float height = dir.y;
dir.y = 0;
float dist = dir.magnitude;
float a = angle * Mathf.Deg2Rad;
dir.y = dist * Mathf.Tan(a);
dist += height / Mathf.Tan(a);
float velocity = Mathf.Sqrt(dist * Physics.gravity.magnitude / Mathf.Sin(2 * a));
return velocity * dir.normalized;
}
Comment
Your answer
Follow this Question
Related Questions
Can't make my AI shoot projectiles with raycast 0 Answers
making a boomerang effect in a 2D enviorment 1 Answer
How to make Cool Goal physics 0 Answers
Shoot towards target 0 Answers
Shooting sideways? 1 Answer