- Home /
Can't get enemy to chase player
I am working on a 2D game in which there is a monster type who, upon instantiation, is supposed to move slowly toward the player. As the player moves and changes direction, the enemy should change course. The player is faster than the enemy, so staying ahead of it won't be a problem as long as the player is paying attention and there aren't too many enemies on the board.
All of the code for the player running, jumping, etc. works just fine, but the enemy isn't moving at all. Here is the code I have:
using UnityEngine;
using System.Collections;
public class Monster3AI : MonoBehaviour {
public Transform player;
private Vector3 playerLocation;
private float pursueSpeed = 200f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update () {
player = GetComponent<Transform>();
playerLocation = new Vector3 (player.position.x, player.position.y, player.position.z);
Debug.Log ("playerLocation: "+playerLocation);
transform.position=Vector3.MoveTowards(transform.position,playerLocation,pursueSpeed*Time.deltaTime);
}
}
This script is attached to a GameObject called Monster3 that is instantiated from time to time. In the inspector, the Player reference says "Player (Transform)", but when I look at the Debug Log, it's showing the location where the Monster3 object was instantiated. I know that using GetComponent() in the Update section is a big performance hit, but I was just trying to get the system to update with the latest location.
I've tried both MoveTowards and Lerp, but neither seems to work.
I'm sure I'm missing something obvious, but if anyone could help, I'd appreciate it!
Answer by RetepTrun · Apr 30, 2014 at 12:14 AM
player = GetComponent<Transform>();
delete that line
your getting yourself
Thanks for this feedback, but now $$anonymous$$onster3 is only going to the initial location where the Player prefab was instantiated. Looking at the Inspector in Runtime, I also notice that, although Player is moving around, the values for transform.position aren't updating. How do I get $$anonymous$$onster3 to get an up-to-date value for Player's location?
Set player in the inspector. it works i tested it.
Youve been overthinking this, you can just set the player once in the inspector
using UnityEngine;
using System.Collections;
public class folowNob : $$anonymous$$onoBehaviour {
public Transform player;
//private Vector3 playerLocation;
private float pursueSpeed = 3;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update () {
//player = GetComponent<Transform>();
//playerLocation = new Vector3 (player.position.x, player.position.y, player.position.z);
//Debug.Log ("playerLocation: "+playerLocation);
transform.position=Vector3.$$anonymous$$oveTowards(transform.position,player.position,pursueSpeed*Time.deltaTime);
}
}
Answer by Datsusara · May 02, 2014 at 06:50 PM
It's still only going to the location where the Player prefab initialized! I'm not sure what I'm doing wrong... When you say, "Set player in the inspector," you mean grab the Player prefab and drag/drop it onto the location in the inspector window where it says "Player" under Monster3AI, correct? I've done that, but it still only wants to go to the default location setting of the prefab and not the current location.