Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
8
Question by Sandr0G · Jun 26, 2012 at 01:11 PM · aienemy

How to make enemy chase player. Basic AI

Hello,

I've made a search in Unity Answers and I didn't find what I wanted, or if I finded, it didn't worked.

I want to make the enemy to chase the player, but I don't know how to do that.

Thank you.

Comment
Add comment · Show 4
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 rpamaral · Mar 04, 2014 at 04:13 PM 0
Share

There's another way to see the target position without using the lookAt? I'm trying to use that for unity 2D and it's not working at all..

avatar image robertbu · Mar 04, 2014 at 04:32 PM 0
Share

@rpamaral - please don't post new questions as 'Answers' to old questions. First an 'Answer' is a solution on UA. Second, since this is really a new question, we ask that you open a new question. As it happens, this questions has been asked and answered a number of times since Unity release their new 2D stuff. Here is one answer:

http://answers.unity3d.com/questions/603757/2d-mouse-ai$$anonymous$$g.html

avatar image RealSoftGames · Oct 10, 2014 at 04:09 PM 0
Share

may i ask how you would add the attacking part for the zombies?

avatar image DeveshPandey · Mar 26, 2018 at 05:08 PM 0
Share

Here is a good plugin, it may help you.

https://www.assetstore.unity3d.com/#!/content/87744?aid=1101l34jr

7 Replies

· Add your reply
  • Sort: 
avatar image
25
Best Answer

Answer by Dee Va · Jun 27, 2012 at 02:53 PM

 var Player : Transform;
 var MoveSpeed = 4;
 var MaxDist = 10;
 var MinDist = 5;
 
 
 
 
 function Start () 
 {
 
 }
 
 function Update () 
 {
     transform.LookAt(Player);
     
     if(Vector3.Distance(transform.position,Player.position) >= MinDist){
     
          transform.position += transform.forward*MoveSpeed*Time.deltaTime;
 
           
           
          if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
              {
                 //Here Call any function U want Like Shoot at here or something
    } 
    
    }
 }
Comment
Add comment · Show 13 · 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 Dee Va · Jun 27, 2012 at 02:54 PM 3
Share

Attach This Script to your Enemy use $$anonymous$$inDist for enemy to stop at certain range and $$anonymous$$axDist For shooting at your player if you have anything like that :)

Hope This Helps You :D

avatar image Sandr0G · Jun 27, 2012 at 06:38 PM 1
Share

Thank you, it worked :D

avatar image conversestar107 · Apr 11, 2013 at 08:06 PM 0
Share

Hi,

I'm trying to use this script as well however I want the level to reload when the enemies get close enough to the player. How would you suggest doing this? I've been trying to add a collider and trigger to the script but it hasn't been working.

Thanks!

avatar image conversestar107 · Apr 11, 2013 at 08:31 PM 1
Share

Never$$anonymous$$d, I was able to figure out but thanks again for the help on the scripting for the enemies!

avatar image robbiepaull1998 · Jul 20, 2013 at 07:48 AM 1
Share

thanks it worked perfectly, but how would i put a walk animation on it

avatar image mapriels robbiepaull1998 · Apr 29, 2020 at 08:13 AM 0
Share

Please make create an Animation for that. And try to use you own Code :D

Show more comments
avatar image
22

Answer by RuggedList17 · Apr 21, 2017 at 07:48 AM

I translated the best answer to C# for all of you C# users out there.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 public class AIController : MonoBehaviour
 {
 
     public Transform Player;
     int MoveSpeed = 4;
     int MaxDist = 10;
     int MinDist = 5;
 
 
 
 
     void Start()
     {
 
     }
 
     void Update()
     {
         transform.LookAt(Player);
 
         if (Vector3.Distance(transform.position, Player.position) >= MinDist)
         {
 
             transform.position += transform.forward * MoveSpeed * Time.deltaTime;
 
 
 
             if (Vector3.Distance(transform.position, Player.position) <= MaxDist)
             {
                 //Here Call any function U want Like Shoot at here or something
             }
 
         }
     }
 }
 
Comment
Add comment · Show 9 · 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 Noah1377 · Mar 21, 2018 at 08:34 AM 0
Share

Thank you!

avatar image ZenBubblez · Mar 26, 2018 at 10:29 PM 0
Share

Your a lifesaver! Thanks a bunch dude!

avatar image ric0030 · May 10, 2018 at 01:47 AM 0
Share

Hey, I'm doing a 2D game for my VCE Game $$anonymous$$aking class, I've added this script into my unity and attached it to my enemy, but when i set it to follow my main character it doesn't follow him and my main guy is right in front of him. Any help?

avatar image unity_wIfM9PlhP0UL9A · Mar 10, 2020 at 02:43 PM 0
Share

Thank you so much for this to work do I name my player Player

avatar image unity_wIfM9PlhP0UL9A · Mar 10, 2020 at 02:48 PM 0
Share

Never $$anonymous$$d I figured out that you need to drag the object into the transform slot but it is just falling off my screen and going invisible

Show more comments
avatar image
4

Answer by Danor · Apr 04, 2017 at 12:46 PM

My version of ChristianBlandford's script, with this the enemies will only look at the player when they are in range, move if they are in range but not further than stop.

 var target : Transform; //the enemy's target
 var moveSpeed = 3; //move speed
 var rotationSpeed = 3; //speed of turning
 var range : float=10f; //Range within target will be detected
 var stop : float=0;
 var myTransform : Transform; //current transform data of this enemy
 function Awake()
 {
     target = GameObject.FindWithTag("Player").transform; //target the player
     myTransform = transform; //cache transform data for easy access/preformance
 }
 function Update () {    //rotate to look at the player
     var distance = Vector3.Distance(myTransform.position, target.position);
     if (distance<=range){
         //look
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
         Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
         //move
         if(distance>stop){
             myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
         }
     }
 }

Comment
Add comment · 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
0

Answer by Berenger · Jun 26, 2012 at 02:29 PM

Simplest AI possible, the zombie :

  • Calculate the vector Zombi -> Player

  • Move (either Translate or AddForce or Move) of that vector deltaTime speed, speed being a var you defined.

To expand that, you'll need to make the AI walk idly until the player is close enough, and again when the player is too far / behind an obstacle. After that, pathfinding.

Comment
Add comment · Show 3 · 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 Sandr0G · Jun 26, 2012 at 03:50 PM 0
Share

Can you give me the code please? I'm a beginner.

avatar image ChristianBlandford · May 30, 2014 at 06:26 PM 0
Share

heres $$anonymous$$e:

 var target : Transform; //cant't be bothered to do any commments
 var moveSpeed = 3; 
 var rotationSpeed = 3; 
 var range : float=10f;
 var range2 : float=10f;
 var stop : float=0;
 var myTransform : Transform; 
 function Awake()
 {
     myTransform = transform; 
 }
  
 function Start()
 {
      target = GameObject.FindWithTag("Player").transform; 
  
 }
  
 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);
     }
  
  
 }
avatar image Itachi_Uchiha · Jun 06, 2015 at 11:02 AM 0
Share

hi I'm new to unity i tried running using the script ,it works but the enemy character seems to be rotating continuously

avatar image
0

Answer by ChristianBlandford · May 30, 2014 at 06:40 PM

Heres Mine:

 var target : Transform; 
 var moveSpeed = 3; 
 var rotationSpeed = 3; 
  
 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 () {
     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;
  
  
 }
Comment
Add comment · 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
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

41 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 avatar image avatar image avatar image

Related Questions

Enemy AI keeps chasing player even after the player has left its line of sight. 0 Answers

How to create an if statement for each element within a list C# 2 Answers

Enemy is not taking damage! 0 Answers

Guard AI acting real weird - Please Help! 0 Answers

Enemy Chasing player? 0 Answers


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