- Home /
How to make an AI to follow the player in 2D C#
So I have tried just about everything to find out how to script an AI following script correctly.
What my script is supposed to do: Always look at the Player. What script does: *When the Player is on the leftside of the Enemy my enemy looks left and vice versa. *It moves up along it's local y-as.
using UnityEngine;
using System.Collections;
public class AiZ : MonoBehaviour {
Transform target;
public float speed = .01f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
target = GameObject.FindWithTag ("Player").transform;
Vector3 forwardAxis = new Vector3 (0, 0, -1);
transform.LookAt (target.position, forwardAxis);
Debug.DrawLine (transform.position, target.position);
transform.eulerAngles = new Vector3 (0, 0, -transform.eulerAngles.z);
transform.position -= transform.TransformDirection (Vector2.up) * speed ;
}
}
Never call Find
function in any of the update functions, it's very resource consu$$anonymous$$g. Call it in start and save the reference in a field.
Answer by sath · Jan 01, 2014 at 06:40 PM
Try this:
using UnityEngine;
using System.Collections;
public class AiZ : MonoBehaviour {
public Transform target;//set target from inspector instead of looking in Update
public float speed = 3f;
void Start () {
}
void Update(){
//rotate to look at the player
transform.LookAt(target.position);
transform.Rotate(new Vector3(0,-90,0),Space.Self);//correcting the original rotation
//move towards the player
if (Vector3.Distance(transform.position,target.position)>1f){//move if distance from target is greater than 1
transform.Translate(new Vector3(speed* Time.deltaTime,0,0) );
}
}
}
So this works half, now the enemy follows my player but it rotates around all 3 axes, so I put
transform.eulerAngles = new Vector3 (0, 0, -transform.eulerAngles.z);
After //rotate to look at the player
So it would only rotate around the z-axis but then the enemy starts moving along the z-axis. To fix this I changed : //move towards the player enemyTransform.position += enemyTransform.forward speed Time.deltaTime;
//Into
enemyTransform.position += enemyTransform.up * speed * Time.deltaTime;`
But then it won't rotate anymore :/
I test the script in an empty scene with Cube(for enemy & this script on it), a Sphere (tag as Player)and it is working fine. Cube(enemy) rotates on Y axis and always follow & looking at Player(sphere).Is this what you want or am I missing something?
I'm working in 2D with a top down view, if I want to rotate an object I need to revolve it around the z-axis
using UnityEngine;
using System.Collections;
public class enemy : $$anonymous$$onoBehaviour {
Transform target;
Transform enemyTransform;
public float speed = 3f;
public float rotationSpeed=3f;
void Start () {
//obtain the game object Transform
enemyTransform = this.GetComponent<Transform>();
}
void Update(){
target = GameObject.FindWithTag ("Player").transform;
Vector3 targetHeading = target.position - transform.position;
Vector3 targetDirection = targetHeading.normalized;
//rotate to look at the player
transform.rotation = Quaternion.LookRotation(targetDirection); // Converts target direction vector to Quaternion
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
//move towards the player
enemyTransform.position += enemyTransform.forward * speed * Time.deltaTime;
}
}
using UnityEngine;
using System.Collections;
public class AiZ : $$anonymous$$onoBehaviour {
Transform target;
Transform enemyTransform;
public float speed = 3f;
public float rotationSpeed=10f;
Vector3 upAxis = new Vector3 (0f, 0f, -1f);
void Start () {
}
void FixedUpdate(){
target = GameObject.FindWithTag ("Player").transform;
}
void Update(){
//rotate to look at the player
transform.LookAt (target.position, upAxis);
transform.eulerAngles = new Vector3 (0f, 0f, transform.eulerAngles.z);
//move towards the player
enemyTransform.position += transform.up * speed * Time.deltaTime;
}
}
This is how far I can go, now the enemy follow my player but only in steps of 90° and the enemy won't go down
Answer by Judgeking · Mar 11, 2017 at 10:06 AM
LookAt won't work in 2D, it makes the sprite flat towards the camera and invisible.
Here's a simple script I wrote:
private GameObject Player;
void Start () {
Player = GameObject.Find("Player");
}
void Update () {
transform.position = Vector2.MoveTowards(transform.position,Player.transform.position, speed*Time.deltaTime);
}
Answer by Narosenla · Jun 12, 2017 at 11:48 AM
public float speed = 0.5f;
public Transform Player;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 displacement = Player.position -transform.position;
displacement = displacement.normalized;
if (Vector2.Distance (Player.position, transform.position) > 1.0f) {
transform.position += (displacement * speed * Time.deltaTime);
}else{
//do whatever the enemy has to do with the player
}
}
}