Enemy That Can Aim!

Ryan McCoach
5 min readJun 27, 2021

--

In this article we are going to cover how to make an enemy that can take aim at a moving player or target.

First, lets create the Enemy game object. The enemy is going to be made up of two game objects: the ship and canon. The ship will being just moving down the screen and the canon will be the object that will be aiming and firing at the player. The canon should move with the ship, so we will make it a child of the ship.

Making the Canon a Child of the Ship

Next, we are going to create a game object (FirePoint) where the laser or bullet will spawn from when it is created. It is important we make it a child of the canon so it will rotate with the canon as it tracks the player.

Adding the Fire Point to the Canon

Lets create a script for this enemy and we need to get a handle on the canon and firepoint, so we will declare a GameObject, serialize them, and assign these game objects in the Inspector.

We will also create a rotation speed variable that will allow how to control how fast the canon will rotate towards the player.

Assigning the Canon and FirePoint in the Inspector

In order to rotate the canon towards the player, we need to know the player’s position. We create a Transform variable for the player’s position and find it in the OnStart.

Getting a handle on the Player’s Position

Once we have the player’s position, we can use it to figure what direction the enemy needs to move towards to face the player. We do this by create a local variable called direction in the Update and subtract the position of the enemy (remember the canon is a child of the enemy, so it will have the same position as the enemy) from the player’s position.

We have the direction the enemy needs to move towards to face the player, so we can now use this to rotate the canon towards the player. We will do this by getting the canons up axis and setting it to a Vector3.MoveTowards.

Canon’s Transform Up

A Vector3.MoveTowards requires the current Vector3 position, the targeted Vector3 position and the distance to be moved between frames.

Canon Rotating Towards Player’s Position

The current Vector.3 position is the canon’s transform up, the target Vector3 is the direction we calculate to get the heading direction, and the distance to move between frames will be the rotationSpeed variable multiplied by Time.deltaTime.

Right now, we have the canon being able to rotate towards the direction of the player as the player moves. Now, we need the enemy to fire in the direction the canon is currently facing.

Laser or Bullet Declared Variables

A couple variables will need to be declared. First, the laser prefab game object which will be serialized so we can assign it in the Inspector. Next, a float that will be amount of time between each shot being fired. Lastly, another float that will store the last time the enemy fired a shot.

Firing Code

In Update, we are checking Time.time (the amount of time that has passed since the game started) to see if it is greater than the last time the enemy fire plus the fire rate. I have another article that goes over this logical more in detail, so click below to read that article.

Once that condition is met, we will instantiate the laser prefab, set the position to the fire point’s position and the rotate to the fire point’s rotation. Remember we calculated the rotation of the canon and the fire point is a child of the canon, so the fire point’s position and rotation will be the same as the canon’s.

Lastly, we set the last time fired to current Time.time.

We will need to create a script that controls the movement of the laser prefab. In this script, we will declared a few variable: a float for the speed of the projectile, a float for the life span of the projectile and a Rigidbody. I have a rotation variable, but that is not needed to make this work. I just have it because I want the projectile to spin when it is moving.

Laser Prefab Declared Variables

Make sure to attach a Rigibody to the laser or projectile and set the gravity to 0, so it doesn’t affect the movement of projectile.

Adding Movement to Laser Prefab

In the OnStart, we will need to get access to the Rigidbody. Once we have a handle on the Rigidbody, we add a force to the Rigidbody.

It requires a position or Vector2, which will set it to the transform.up, which is what is being move towards the player and multiply it by our speed variable. The speed variable should be in the hundreds to see any significant movement. We want to destroy the laser or projectile prefab after some time so it doesn’t eat up memory. Using the Destroy will pass in the game object (the laser) and the amount of time we want to wait before having it destroyed, which is our life span variable. This takes care of the laser or projectile script.

Here is the complete Enemy and Projectile Script…

Enemy Script
Laser or Projectile Script

Cheers!

--

--

No responses yet