Enemy able to chase the player

Hi, I’d like to create a platform game, so I attended a Udemy course (https://www.udemy.com/unity2dplatformer). This is a good course, it explains how to create a patrolling enemy between 2 points. I’d like to improve it, so that when the player gets close to the enemy, the enemy begins to chase him and eventually changing direction to go towards him.

I found a way to start chasing the player, using only one axis (x). This is the code:

if ((Mathf.Abs(transform.position.x - target.position.x) < 5f) && (Mathf.Abs(transform.position.y - target.position.y) < 1f))

{

float step = moveSpeed * Time.deltaTime;

transform.position = Vector3.MoveTowards(transform.position, target.position, step);

}

I don’t know how to implement this when the enemy is patrolling. If the enemy is going towards right, and the player is on his left, the enemy doesn’t turn towards the player.

Here is the code of the enemy script:

using UnityEngine;

using System.Collections;



public class EnemyController : MonoBehaviour {



public Transform leftPoint;

public Transform rightPoint;



public float moveSpeed;



private Rigidbody2D myRigidbody;



public bool movingRight;



// Use this for initialization

void Start () {

myRigidbody = GetComponent<Rigidbody2D>();

}


// Update is called once per frame

void Update () {



if(movingRight && transform.position.x > rightPoint.position.x)

{

movingRight = false;

}

if(!movingRight && transform.position.x < leftPoint.position.x)

{

movingRight = true;

}



if(movingRight)

{

myRigidbody.velocity = new Vector3(moveSpeed, myRigidbody.velocity.y, 0f);

} else {

myRigidbody.velocity = new Vector3(-moveSpeed, myRigidbody.velocity.y, 0f);

}

}

}

How can I do? Please, help me!

Thanks very much to all those who can solve my problem.

Please read this page for the future and to edit your post:)

It shows you how to insert code nicely on the forums.

As for your question, do you want the enemy to chase the player if it’s beyond the right / left points? (if they the player can be beyond those points). This might require a bit more logic.

I would say make sure you continue to use velocity for physics, rather than move towards.

Just check if you’re chasing, turn to face (use similar code that you have for your right point/ left point), and set ‘facingRight’ appropriately.
Then, include the check for out of bounds, if they can’t chase outside of that.

Thanks for your advice. I solved it! This is the code:

void Update()
    {
       
        if (movingRight && transform.position.x > rightPoint.position.x)
        {
            movingRight = false;
        }
        if (!movingRight && transform.position.x < leftPoint.position.x)
        {
            movingRight = true;
        }

        if (movingRight)
        {
            myRigidbody.velocity = new Vector3(moveSpeed, myRigidbody.velocity.y, 0f);
        }
        else
        {
            myRigidbody.velocity = new Vector3(-moveSpeed, myRigidbody.velocity.y, 0f);
        }
       
        if ((Mathf.Abs(transform.position.x - target.position.x) < 5f) && (Mathf.Abs(transform.position.y - target.position.y) < 1f))
        {

            if(target.position.x >= transform.position.x && target.position.x < rightPoint.position.x)
            {
                myRigidbody.velocity = new Vector3(moveSpeed, myRigidbody.velocity.y, 0f);
            }

            if (target.position.x <= transform.position.x && target.position.x > leftPoint.position.x)
            {
                myRigidbody.velocity = new Vector3(-moveSpeed, myRigidbody.velocity.y, 0f);
            }

        }

If I would the enemy to start chase immediately after the player touches the platform, how can I do? Shoud I use Layer or Trigger?

Is that a little bit in contradiction to the distance of less than 5? :slight_smile:

You could always chase, perhaps, anytime the distance between the player’s y position and the enemies is less than a certain amount, if you stop caring about the x.

It’s not clear what you’re hoping for, atm (to me).

1 Like

Good solution! Thank you :slight_smile:

Okay, cool… Glad I could help :slight_smile: