Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jan 09, 2014 at 02:07 AM by AlucardJay for the following reason:

The question is answered, right answer was accepted

avatar image
4
Question by MonkeyAssassin8 · Aug 29, 2010 at 06:34 AM · aifollowbasic

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AndrewBilotti · Jan 14, 2016 at 01:34 AM 0
Share

Cool! Great question :P

2 Replies

  • Sort: 
avatar image
20
Best Answer

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.

Comment
Add comment · Show 32 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jesus_Freak · Oct 25, 2010 at 02:13 PM 1
Share

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.

avatar image DeathList702 · Jun 16, 2012 at 07:17 AM 0
Share

Great!But why the enemy clipping the walls ?(sry for my english)

avatar image wolga · Jun 28, 2012 at 04:02 PM 0
Share

using this script, how could i make the enemy follow me and stop with the distance to me of 5 unity? thanks a lot!

avatar image Nevadaes wolga · Jul 20, 2012 at 10:59 PM 0
Share

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.

avatar image soxroxr · Jul 17, 2012 at 07:20 PM 0
Share

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.

avatar image charlietorres · Jul 20, 2012 at 02:41 PM 0
Share

excellent script!! it works perfectly :D 100 points! :P

Show more comments
avatar image
4

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);
     }
     
  
 }
Comment
Add comment · Show 16 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Xeina15 · Jan 20, 2014 at 06:12 PM 0
Share

Thanks so much! super helpful for my project.

avatar image maxjadin · Feb 26, 2014 at 03:36 AM 0
Share

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?

avatar image ThatAwesomeDude · Apr 14, 2014 at 03:20 PM 0
Share

$$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.)

avatar image ChristophtheGamer0042 · May 14, 2014 at 02:20 PM 0
Share

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.

avatar image Suicune · May 21, 2014 at 02:22 PM 0
Share

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?

Show more comments

Follow this Question

Answers Answers and Comments

38 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges