Your source code is now downloading. If does not start download please wait for 10 seconds

Your source code is now downloading. If does not start download please wait for 10 seconds

Objective Main objective of this blog post is to give you basic idea about Enemy Follow Script (AI) in Unity.

You will get final output:

Step 1 Introduction

This script can be useful in cases when one wants an enemy object to follow the player, or other object that it wants to target. It will create a leaping effect while moving towards the targeted object. It would look as if the enemy is thinking by itself and trying to catch the targeted object.

This kind of AI is very useful in case of action games, where the enemy follows a player and tries to shoot or attack. Also in case of Car chasing games, where the follower may be playing police like character.

A simple example to explain this kind of script is as given below

Follow the steps given below:

Step 2 Demo

2.1 Project Setup

Create a Cube and place it over a plane with appropriate size.

Apply RigidBody to the Cube.

This Cube will act as an object which would be moving on the player’s commands.

Freeze Rotations on X and Z Axis to avoid inappropriate effects while moving.

Apply box collider and materials as per the requirements.

Now Apply the Script of CharacterMovement as given below.

2.2 CharacterMovement.cs

public class CharacterMovement : MonoBehaviour { public float moveSpeed=10.0F; void Update () { //Get Input From User Vector3 input = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical")); //Apply Force as per requirement rigidbody.AddForce (input*moveSpeed); //NOTE:- Applying force in fixed update is recommended for better effect because Physics would be applied at fixedrate gives better results //To Give a Jump Effect if (Input.GetKey (KeyCode.Space)) { rigidbody.AddForce(Vector3.up,ForceMode.Impulse); } } }

2.3 Create Enemies

Create three Spheres which would act as enemies who are meant to follow the player on trigger

Apply RigidBody to the Spheres.

Apply Sphere Collider and Materials as per the requirement.

Apply FollowScript as given below to all the Spheres.

Speed can be manipulated from inspector as given below.

Different enemies usually have different following speed.

2.4 FollowScript.cs

public class FollowScript : MonoBehaviour { public Transform Character; // Target Object to follow public float speed=0.1F; // Enemy speed private Vector3 directionOfCharacter; private bool challenged=false;// If the enemy is Challenged to follow by the player void Update () { if (challenged) { directionOfCharacter = Character.transform.position - transform.position; directionOfCharacter = directionOfCharacter.normalized; // Get Direction to Move Towards transform.Translate (directionOfCharacter * speed, Space.World); } } // Will be triggered as soon as player would touch the Enemy Object voidOnTriggerEnter( Collider other) { if (other.CompareTag ("Player")) { challenged=true; } } }

Note Keep different speed for each sphere to differentiate them (For these three spheres I’ve used 0.1, 0.05, and 0.075)

Hence as soon as the player touches or challenges the enemy, the enemy will start to follow the player. One can change the challenge condition as per the need.

One can also increase the following speed in the script in conditions where it is mandatory that the follower should be able to catch the target object.

Along with this code one can add rotation to enemy object such that it faces towards the target object.

Keep Hierarchy as given below:

I hope this blog would help you in creating better and efficient enemies in your game. Modifications can be made to the script as per requirement. Feel free to comment here if you need any help. I will reply you ASAP.

Got an Idea of Game Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Game Development Company in India.