How to make a rocket follow a parabolic curve when dumb fired

Hello,

So I am making an arcade racing game inspired by Re-Volt and I’m making a firework that can track an enemy after locking on, but I also want it to be dumb fired.

When dumb firing it, i want it to follow a simple parabola but I don’t think i want it to be fully scripted. The reason i think I don’t want that is because if (for example) it flies and hits an object, i want it to somewhat realistically bounce off of that object and continue following whatever trajectory it has then.

Two ways:

  • keep the parabola, each frame steer the rocket towards the next point on the parabola by adding forces.
  • Calculate the required initial forces to follow the parabola, fire and forget. Start here.

The second one is probably the easiest if you only have gravity working on the rocket. If it’s being affected by drag and acceleration and stuff like that, it gets somewhat harder, and steering roughly towards the next point on a spline might be less of a headache

Add a Rigidbody to the rocket object. Leave “use gravity” checked and “is kinematic” unchecked. Enable continuous collision detection since rockets typically travel at high velocities. Enable interpolation (dropdown) to ensure rocket motion is smooth (default FixedUpdate is 50 Hz).

Add a Capsule Collider to the rocket object. Leave “is trigger” unchecked. Ensure collider roughly aligns with the rocket mesh.

When firing/launching, get or use the Rigidbody and call rigidbody.AddForce(launchForce, ForceMode.Impulse) once.

To ensure the rocket doesn’t tumble but always heads “forward”, add a simple “rotate forward” script that rotates the rocket based on its current velocity (provided it flies fast enough):

    private void FixedUpdate()
    {
        var flightDirection = rb.linearVelocity;

        // Only rotate if the rocket is moving fast enough ...
        if (flightDirection.sqrMagnitude > minSpeedToRotate * minSpeedToRotate)
        {
            // Get a rotation facing the flight direction ...
            var targetRotation = Quaternion.LookRotation(flightDirection);

            // MoveRotation ensures rotation is smooth and interpolated
            var speed = turnSpeed * Time.fixedDeltaTime;
            var rot = Quaternion.Slerp(rb.rotation, targetRotation, speed);
            rb.MoveRotation(rot);
        }
    }

Tweak the values as you see fit. Start with minSpeedToRotate = 1 and turnSpeed = 5. The launchForce depends on Rigidbody mass, for mass = 1 a good starting value is likely between 10-30. Higher mass => higher launch force. Also add the firing car’s current linearVelocity on top, perhaps with a multiplier, so that the rocket’s speed launches with a speed relative to the motion of the firing car. It would likely feel odd otherwise.

You could add a timer to make it stop rotating in forward direction which should coincide with stopping rocket trail particles or trail renderer to make it seem like it burned up its fuel.

For explosion you could also add a Sphere collider on the tip of the rocket, slightly bigger than the capsule collider. Set it to “trigger”, and use only OnTriggerEnter to explode the rocket. With a bigger sphere collider you will make it easier to hit targets.

Make the trigger sphere noticably bigger (ie 10x the size of the rocket) and the rocket would seem like having a proximity trigger, ie like anti-air shells. Such a proximity-triggered rocket if fired cleverly in front or to the side of an opponent, might not do much damage but the explosion force would slow down the opponent or push them off track. That seems like fun. :slight_smile:

PS: Make sure the firing car is always excluded from triggered explosions, or the rocket is on a timer and won’t explode for 1 second (safety-first, but can still hit yourself). :wink: