- Home /
The question is answered, right answer was accepted
How to create a basic follow AI
I don't know much about scripting and was wondering if anyone knew a basic script to get an enemy to follow the player. I've tried using the one on the tutorial, but when putting it into my own game, it doesn't work. Help would be very much appreciated.
Answer by GesterX · Aug 29, 2010 at 10:00 AM
The code below should get an enemy to follow your player. No triggers are set up so the enemy will follow the player no matter how far it is away. This should be a good starting point. Read all the comments.
var target : Transform; //the enemy's target var moveSpeed = 3; //move speed var rotationSpeed = 3; //speed of turning
var myTransform : Transform; //current transform data of this enemy
function Awake() { myTransform = transform; //cache transform data for easy access/preformance }
function Start() { target = GameObject.FindWithTag("Player").transform; //target the player
}
function Update () { //rotate to look at the player myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
All you need to do is create a new script and copy the above code into it. You need to tag your Player with the "Player" tag. Then just drag the script onto the enemy you want to follow the player.
This should be a good starting point for you to script more complex behaviour.
If you haven't already I would check out the Lerpz Platformer tutorial in the Unity3d site. It will take you through a lot of basic concepts and comes with a lot of scripts you can reuse.
wow, thats a cool script, the enemy actually follows my player! and i have a seperate script for turret shotting and dying, so its cool to have a script for following my player.
Great!But why the enemy clipping the walls ?(sry for my english)
using this script, how could i make the enemy follow me and stop with the distance to me of 5 unity? thanks a lot!
To make a very simple addition to this script, all you'd have to do would be to enclose the content of Update() in a "if" statement checking if the distance between the two agents is greater than 5.
As a re$$anonymous$$der, the distance is equal to the magnitude of the vector represented by the difference of position of the agent following the player and the player's.
The enemy is clipping the walls because this goes at your player in a straight line, no matter if there is anything in the way or not. As stated, it's a very basic AI.
excellent script!! it works perfectly :D 100 points! :P
Answer by Ichan · Aug 09, 2013 at 02:09 AM
Just a small update on this old post made it simple so if will look at you from a certain distance and if you get closer then a certain distance it follows. i made range and range 2 so its easier to use ok :D
edit added a stopping distance so the enemy wont keep going though you and circling
var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var range : float=10f;
var range2 : float=10f;
var stop : float=0;
var myTransform : Transform; //current transform data of this enemy
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
}
function Update () {
//rotate to look at the player
var distance = Vector3.Distance(myTransform.position, target.position);
if (distance<=range2 && distance>=range){
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
else if(distance<=range && distance>stop){
//move towards the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
else if (distance<=stop) {
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
}
is there anyway you can make it so when you look at the enemy it will stop then when you look away it will follow you?
$$anonymous$$y enemy won't follow unless I have a rotation on, I can't put this on 0 otherwise it won't work. I tried deleting it from the Javascript but that didn't work either. (I'm using this for a 2D game so as a sprite it shouldn't rotate.)
I used your script and it works perfectly, but now I want to make the stop variable into a random integer. How exactly could I do that? Thank you.
Everytime I try to play the game with this script attached to my enemy, I keep getting the same Error
NullReferenceException: Object reference not set to an instance of an object Follow.Start () (at Assets/Standard Assets/Scripts/Follow.js:15)
What do I do?
Follow this Question
Related Questions
Problems with simple AI script 3 Answers
Change AI Follow Players 1 Answer
AI Script Problem, Horror Game 0 Answers
How to implement Aggro into a AI script (Js) 0 Answers
Maze navigation 1 Answer