- Home /
C # script the enemy chases the player.
Hello everyone.
I have this script that does not work for the enemy. When the player approaches the enemy, it is not pursued. The enemy remains still in place. What could be the problem?
Script of the enemy:
using UnityEngine;
using System.Collections;
public class EnemyInseguePlayer : MonoBehaviour {
public Transform player;
public float playerDistance;
public float playerXAxis;
public float moveSpeed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
playerDistance = Vector3.Distance (player.position, transform.position);
if(playerDistance < 15f)
{
LookAtPlayer ();
}
if(playerDistance < 18f)
{
chase ();
}
}
void LookAtPlayer()
{
Vector2 playerXAxis;
playerXAxis = new Vector2(player.position.x, player.position.y);
}
void chase()
{
transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
}
}
Answer by komodor · May 21, 2015 at 01:24 PM
instead of using method LookAtPlayer (which doesn't do anything) use http://docs.unity3d.com/ScriptReference/Transform.LookAt.html
and maybe check your moveSpeed because the chase should work if it's different from 0
if (playerDistance < 15f)
{
transform.LookAt(player.transform);
}
if (playerDistance < 18f)
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
Answer by chintan_shroff · May 21, 2015 at 03:33 PM
Few issues I would like to point out... Check if you have assigned the player in the editor. Second, LookAtPlayer() function will do nothing because you are not using playerXAxis variable to change the orientation of the enemy. Also I do not understand why would an enemy chase you first and then look at you. But this is upto your logic
if(playerDistance < 15f)
{
LookAtPlayer ();
}
if(playerDistance < 18f) //imo, change this to a value below 15
{
chase ();
}
Third you have declared playerXaxis as a float and then as a vector2 in our LookAtPlayer() function.
I was unable to figure out the issue with your code(i think its the distance bit) but here is another simple solution. Create 2 triggers around the enemy gameobjects as child objects and then check for OnTriggerEnter2D on the player script for each to get the desired output.
void OnTriggerStay2D(Collider2D other)
{
if (other.gameObject.name == "Chase Trigger"){ //ensure you name the triggers correctly
enemy.Translate (Vector3.up * speed * Time.deltaTime, Space.World);}
else if (other.gameObject.name == "Look Trigger"){//ensure you name the triggers correctly
//lootAtEnemy();
}
couple of things I missed here
be sure to set the speed variable in the inspector else it will be 0 and there will be no translation
also, use Vector3.up if you are making a 2D game. Vector3.forward will move your gameobject in the Z axis
I get an error in enemy.
error CS0117: Enemy' does not contain a definition for
Translate'
make a public Transform variable called enemy, in your player script and make sure to add your enemy to the player script in the inspector
Your answer
![](https://koobas.hobune.stream/wayback/20220613200127im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Enemy backing away from player after going around corner? 1 Answer
Enemy is not stopping following the player 2 Answers
Enemy reorienting for player 1 Answer
Camera rotation around player while following. 6 Answers
Enemy to follow the Player 1 Answer