Objective: Create guided missiles for the player to use!
We have already created targeting missiles for the enemies. We now want to give the player a number amount of missiles to add to their arsenal. The beginning of the process we need to create a physical object to represent the missile. If you are using the same assets, I used then there are different color missiles you could use.
We need to create a new C# script for the player’s missile.
We need to add components to the missile. Rigidbody, collider, animator, and audio source.
Inside the script we need to create a list of variables. We need floats for speed, and minimum distance. Game Objects for the target and the missile explosion. An animator, audio source and Enemy.
In the start method we need get components to the animator, audio source, and the enemy.
We need to create a return method for the missile to find enemies. It’s the same method we used on the enemy missile.
Now we need a method to move towards the enemy. Still the same method we discussed before.
We want to call this method in the update method.
Having a missile fly forever is not what we want here, so, we need to create a coroutine that will handle a self-destruct on the missile.
This will cause the missile to self-destruct 2 seconds after it was instantiated.
Speaking of instantiating we need to move to the player script and work on instantiating the missile. First, we need to create a game object variable for the missile prefab and attach the prefab in the inspector.
In the update method, where we put all our input code, we want to attach the instantiate code to the input key F.
Now whenever we press the F key, we will instantiate a missile and it will either travel to the upwards or towards an enemy.
In the next part we will set limits such as ammo and a powerup to give the player more missiles. As I always say, don’t be afraid to experiment with your code, and I’ll see you in the next tutorial!