0
\$\begingroup\$

In my project, I have a ship that moves and increases it's movement speed with respect to time and how long the acceleration button is being pushed(just like the way a real life car works), now as expected, the ship fires projectiles when I press the fire button.

The problem here, is that at a point in the game, the ship reaches it's highest speed/terminal velocity, and now it's faster than the projectile it fired which is not what I want, so, how do I create a realistic projectile movement based on the ship's velocity? This is so that if the projectile is fired when the ship is moving slowly through the game scene, it doesn't move so fast that we can't see it travelling in the forward direction, and at the same time if it is fired when the ship is moving at top speeds, the ship is not seen to be faster than or as fast as the projectile's speed.

I want to keep the code as optimized as possible, so I am using this line for the projectile's movement:

public float velocity = 100f;
void Update() {
transform.position += transform.forward * Time.deltaTime * velocity;

}
\$\endgroup\$
  • \$\begingroup\$ Should be very straight forward you have two options. 1. If you want the projectile to accelerate as the ship accelerates then you need to add the difference in the ships velocity from the projectiles. 2. If you do not want the projectile to unnaturally accelerate after firing you should have a starting speed slightly greater than the terminal velocity of the ship. If this works for you I'll add it as an answer. \$\endgroup\$ – STEAMworks Learning Center Nov 21 at 12:09
  • \$\begingroup\$ "want to keep the code as optimized as possible" then you should probably use a rigidbody for the projectile. The physics engine's internal velocity integration is likely more optimized than doing the vector math in managed code — especially if you're relying on the physics engine for detecting collisions. Keeping the movement in the physics engine's control can help it avoid unnecessary recomputations. \$\endgroup\$ – DMGregory Nov 21 at 13:55
  • \$\begingroup\$ @STEAMworks Learning Center, thank you for the help. \$\endgroup\$ – MetaMax Nov 21 at 13:58
  • \$\begingroup\$ @DMGregory, I used rigidbody when I was running simulations, it wasn't as performance friendly as I thought. Maybe it's from my code, I'll review it and see what's wrong. Thanks for the notice. \$\endgroup\$ – MetaMax Nov 21 at 14:00
  • \$\begingroup\$ Interesting note is that this was a real problem with Grumman F-11 and other fighter jets that can/could outrun their own cannon rounds. Fixing this involved, in part, being trained to pull away from the thing you're firing at (essentially strafing fire but against air targets.) I mention this partly because it could be an interesting game mechanic. \$\endgroup\$ – blurry Nov 21 at 18:10
0
\$\begingroup\$

On your ship, you should have some sort of instantiation for the bullet, or wherever else you have it. Let's say you're instantiating your bullet like so:

{
   GameObject bullet = GameObject.Instantiate(bullet, transform.position, transform.rotation);
}

Instead of doing just this, give its velocity an initial value, like so:

{
   GameObject bullet = GameObject.Instantiate(bullet, transform.position, transform.rotation);
   bullet.velocity = bullet.velocity + ship.velocity;
}

Since you've changed your velocity value, it should now use that value instead. You should have some way to calculate the velocity of your ship (or ideally, you already have the velocity of the ship stored on a script on the ship itself).

If your ship is going -100f and your bullet's speed is 100f, your bullet will stand still, but that's actually just how physics works. So, destroy the bullets after some time. Remember, even if a bullet leaves the screen and cannot be seen by the camera, it still exists in game.

\$\endgroup\$
  • \$\begingroup\$ OK, this is only if it has a rigidbody on it, I want one without a rigidbody. \$\endgroup\$ – MetaMax Nov 21 at 13:06
  • \$\begingroup\$ @MetaMax This code has nothing to do with rigidbodies. I'm talking about the velocity variable that is already in your code. \$\endgroup\$ – John Hamilton Nov 21 at 13:46
  • 1
    \$\begingroup\$ Right, my ship's movement is a rigidbody movement, so I thought ship.velocity was referring to it's velocity. Nevertheless thanks, the projectile's moving at the right speed with the ship. Thank you. \$\endgroup\$ – MetaMax Nov 21 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.