19 votes
Accepted

How can I launch a GameObject at a target if I am given everything except for its launch angle?

I'm a bit skeptical of using atan here, because the tangent ratio shoots off to infinity at certain angles, and may lead to numerical errors (even outside of the ...
DMGregory's user avatar
  • 132k
12 votes
Accepted

Why aren't game bullets implemented as if they were real bullets?

The main problem is tunneling. Game physics engines typically use discrete collisions. That means they advance all the bodies by one short time step, then check if they're intersecting, and handle ...
DMGregory's user avatar
  • 132k
12 votes
Accepted

Projectile Aim Prediction with Acceleration

To make things a little simpler, let's work within the inertial frame of our launcher. That means our projectile will be firing from the point (0, 0, 0). (You can subtract any arbitrary firing ...
DMGregory's user avatar
  • 132k
10 votes
Accepted

Guided Missile with Constant Time

The edit is reassuring. :) Okay, here's a straightforward update loop... Assuming when we fire the missile we initialize remainingFlightTime = 5f then... ...
DMGregory's user avatar
  • 132k
6 votes
Accepted

How to create a projectile motion of a cannonball in top down view?

From wikipedia: $$d=\frac{v_0^2}{g}sin(2\theta)$$ When you reverse the formula to find the angle from the distance and initial speed, it becomes: $$\eqalign{ d&=\frac{v_0^2}{g}sin(2\theta) \\ ...
starikcetin's user avatar
  • 5,245
5 votes
Accepted

How can I recreate the movement of arrow in air?

You need a bit of physics, and a touch of calculus. Lets assign P as the point of the arrow, L as the length of the arrow, and Q as the tail of the arrow. You launch the arrow at an initial velocity ...
Mike White's user avatar
5 votes
Accepted

Destroy object on Collision

You want the 2D version of the method: OnCollisionEnter2D Collisions with a Collider2D/Rigidbody2D won't send the "OnCollisionEnter" message, only "OnCollisionEnter2D"
DMGregory's user avatar
  • 132k
5 votes

What is a "Projectile"?

From dictionary.com: projectile[ pruh-jek-til, -tahyl ] noun an object fired from a gun with an explosive propelling charge, such as a bullet, shell, rocket, or grenade. a body ...
Philipp's user avatar
  • 118k
4 votes
Accepted

How do I create a rocket projectile that is "dropped", then accelerates toward the enemy?

A simple way to achieve this effect would be to launch the missile with an initial velocity of (ship's backwards vector + ship's rightwards vector), then apply a constant acceleration of (ship's ...
4026's user avatar
  • 91
4 votes
Accepted

Calculating initial velocities given trajectory parabola

To simplify the math, let's translate everything so the projectile starts at the origin (subtract player position from the other points). Translating the whole problem doesn't modify the velocity, so ...
DMGregory's user avatar
  • 132k
4 votes
Accepted

Javelin throw projectile motion

To align a javelin, you should properly compute all forces. The reason that a javelin can orient itself into its path, is because of air drag. Note that the drag is not always asymmetric enough to ...
Bram's user avatar
  • 3,729
3 votes
Accepted

Scrolling background after firing projectile

Declare an x value for your camera, and then offset everything (projectile, scenery, etc.) by that camera's X. For example, If I want my camera to be 500 pixels to the right, but projectile is 600 ...
tyjkenn's user avatar
  • 2,576
3 votes
Accepted

How to make an enemy projectile head to the player's position with constant speed

IF I understood your question correctly (it's a bit un-detailed and I recommend editing it with more information and the piece of the code that you tried but is not working), you can solve the problem ...
MAnd's user avatar
  • 4,907
3 votes

Elastic Circle-Circle collision detection and response / Slime Volleyball physics

Basically, you want to implement circle-cirle collision detection and response. I consider your character a static object and the ball a dynamic object. I do this because it makes everything a lot ...
Bálint's user avatar
  • 14.8k
3 votes

How can I launch a GameObject at a target if I am given everything except for its launch angle?

Thanks to DMGregory, I now have a C# extension script which can be used for this. Most recent version can be found on GitHub. ...
Evorlor's user avatar
  • 5,775
3 votes

Shooting projectiles towards mouse position in Unity 2D

Your problem here is two fold: First, when you spawn an object, that should be the last the current script interacts with it. From that point forward, the new object should control its own behavior. ...
Stephan's user avatar
  • 1,728
3 votes

Javelin throw projectile motion

Have you ever thrown a javelin straight up? It does actually rotate quickly on top, because the pointy end is heavier. But it does so not when it reaches the top but after the velocity changes to move ...
Josh Vander Hook's user avatar
3 votes
Accepted

Find projectile initial velocity, and air time, given projectile elevation, distance, angle

If your projectile is fired on a 45 degree angle, that's: $$ \vec v = (c, c)$$ ...for some scalar component \$c\$. Gravity doesn't affect the horizontal axis, so the time it takes this projectile to ...
DMGregory's user avatar
  • 132k
3 votes
Accepted

How to make mouse pointer be the vertex of a drawn parabola when estimating the trajectory of a projectile?

I think we can do this more simply. This demo uses about 80 lines of code (the interesting ones being just the dozen below), instead of over 300, with no angles/trigonometry functions needed: At the ...
DMGregory's user avatar
  • 132k
2 votes
Accepted

Calculate initial velocity for trajectory given duration, launch angle, and distance

Note: the situation you describe (angle, start, end, and time of flight) is overdetermined. For many (most) inputs, there will be no solution that simultaneously satisfies all of these constraints. (...
DMGregory's user avatar
  • 132k
2 votes

How can I launch a GameObject at a target if I am given everything except for its launch angle?

I modified DMGregory and Evorlor's solution a bit as I wanted a 3D arc and wanted to be able to apply a minimum speed if the provided speed wouldn't reach. ...
Tim's user avatar
  • 21
2 votes
Accepted

Required initial velocity (fixed launch angle) for projectile to hit a target

[Reposted as an answer, at the asker's request] turretAngle is in degrees, so you need to convert it to radians before passing it to ...
Victor T.'s user avatar
  • 1,381
2 votes

Why aren't game bullets implemented as if they were real bullets?

For most games it doesn't make a difference. Take the Counter Strike series, for example. It simulates firearms combat on rather short distances. On the distances where the average CS shootout takes ...
Philipp's user avatar
  • 118k
2 votes
Accepted

Rifle bullets/projectiles in Unity, raycasting or rigidbody?

A simple raycasting would be instantaneous. This is usually referred to as Hitscan. A simple example would be in Doom, some weapons and enemies hit immediately while others can be dodged after they ...
CLo's user avatar
  • 1,330
2 votes
Accepted

how to calculate initial Y velocity required to reach target?

You are going to need to use the Equations of Motion. This is because the velocity is not constant over the time frame. You cannot calculate distance traveled by velocity and time alone. One way to ...
Jay's user avatar
  • 800
2 votes

Pushing Rigidbody forward doesn't go straight

Those calculations you made are taking into account a direction that is forward of the camera center. But you are instanciating the grenade in an arbitrary transform called ShootPoint. so how to ...
Omar Guendeli's user avatar
2 votes

Javelin throw projectile motion

maybe you should position the javelin in such a way, that the 'nose' of the javelin is where the curve is, not in the middle, that way it might look a little 'more natural'. or you can add a turning ...
DrDragonite's user avatar
2 votes
Accepted

How do I launch a ballistic projectile to hit a moving target, given: launch position & angle, target position & velocity?

I'm assuming that your gravity acts on the vertical axis, and that your launch angle is an altitude (measured vertically from the horizon, so 0° means firing horizontally, and 90° means firing ...
DMGregory's user avatar
  • 132k
2 votes
Accepted

Find the direction a Projectile should be fired from a Moving Object to hit another moving Object?

This is the updated code, with code for handling some edge cases. Also, I made sure the BULLET_SPEED large enough for the Equations to work: ...
Vignesh Gunasekaran's user avatar
2 votes

Emulating 3d trajectory in top-down 2d game?

I created this version which uses a target position and t (time 0-1). I would loop this function in an update function if (t < 1) t would increase by ...
Joel Leonard-Best's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible