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.
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..
@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
may i ask how you would add the attacking part for the zombies?
Here is a good plugin, it may help you.
https://www.assetstore.unity3d.com/#!/content/87744?aid=1101l34jr
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
}
}
}
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
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!
Never$$anonymous$$d, I was able to figure out but thanks again for the help on the scripting for the enemies!
thanks it worked perfectly, but how would i put a walk animation on it
Please make create an Animation for that. And try to use you own Code :D
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
}
}
}
}
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?
Thank you so much for this to work do I name my player Player
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
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;
}
}
}
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.
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);
}
}
hi I'm new to unity i tried running using the script ,it works but the enemy character seems to be rotating continuously
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;
}