Learn to code by making a 2D space shooter game.

Suleiman Abdullah
5 min readFeb 25, 2022

Objective: Implement Secondary weapon / Implement Heat Seeker Missile.

Before doing that take missile drag to the hierarchy add rigid body set gravity scale to zero add 2D box collider 2D check is triggered.

The first step creates a script called Missile and attaches it to the Missile sprite on the hierarchy.

The second step takes the Missile and drags it to the Project folder to make it a prefab.

Open the Missile scripts then create a variable of GameObject named target, this is the target position don't forget to serialized it to be able to drag Target prefab on the Inspector.

In the next step Cache this component in the start method, we use game GeameObject.findGameObjectWithTag to find the target.

The next step creates a rigid body component and caches the component in the start method.

Now we need to float variable for speed and rotating amount for our missile, give speed the value of 4 and rotating speed the value of 200.

In the next step create a method called CalculateTarget, this will be a method to follow the target in any direction.

In this step, we need to calculate the direction to the target we do this take the target position minus the source position, remember to cast this into Vector2 since the transform is Vector3.

Then we need to normalize direction since we are not interested in the magnitude between these 2 Vectors.

Note: Normalize Vector will give you smooth velocity in some scenarios otherwise velocity will change according to the change of the distance which is weird.

Now we need to do cross product between the basic vector or look direction of the missile and direction we do this by Vector. Cross method, this method will return the third vector perpendicular to both two Vectors.

Note: basic vector is the vector in local space of the missile which transform.up, vector3.up is different since it's in world space its origin its on zero in XYZ.

To be able to use our third vector to turn to the target we need to store the cross product result in to float variable called dot. Doing this will get an error because we need to include z in the cross product.

It's time to turn the missile to the target we use rigid body property called angularVelocity by setting the dot result as the assigned Vector to rotate.

If we try to play like this the missile will go opposite the target to make the missile faces to target direction we need to multiply a negative to the cross value.

We need the missile to the look direction which is transform. up, we move by using rigid body property called velocity then multiply with the speed variable.

We need to call the CalculateTarget method to the FixedUpdate, we do this because we are working with physics components.

Drag the target component to the target field by selecting missile < target on the inspector.

Now open the Player script create a missileprefab game object, and serialized it.

Then create two float variables called missileFireRate and canFiremissile set the value corresponding five to -1.

Create a method called FireMissile, then add fire rate calculation we do this by taking can fireMissile equal to Time. time plus missileFireRate.

It's time to fire the missile we do this by Instantiate method, this will spawn the missile at default position but we need to adjust it to the local position of a spaceship by adding a new Vector3(-0.72,-0.43,0).

Note: show we need to adjust -0.72 in x in local space and -0.43 in y in local space of the ship.

Near the end, we need to fire the missile by checking the keycode and if the time is allowed us to fire the missile again.

Last make the missile a prefab and drag the missilePrefab to the field of the on the Player < missilePrefab.

Bonus I have added this to destroy the enemy and add points to the player score when we hit a target(Enemy).

Let's see the result.

See you in the next article.

--

--

Suleiman Abdullah

Self taught Unity Developer, who is passion about making games ,and he loves Math For Game Development