very confused

I’m new to Unity and game dev in general, as I’m sure most of the people who come here begging for help are. I’ve been trying to get basic AI working for a simple 2D platformer. I want this enemy to notice the player once they get close enough and then follow the player until it reaches them. So far I’ve gotten everything to work except having the enemy follow across both the X and Y axis. With the code

public class RocketScript : MonoBehaviour
{
   [SerializeField]
    Transform player;

   public float agroRange;
   public float moveSpeed;

    Rigidbody2D rb;



    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();

    }

    // Update is called once per frame
    void Update()
    {

        float distToPlayer = Vector2.Distance(transform.position, player.position);
      

        if(distToPlayer < agroRange)
        {
            ChasePlayerHorizontal();
        }
    }

    private void ChasePlayerHorizontal()
    {
        if(transform.position.x < player.position.x)
        {
            rb.velocity = new Vector2(moveSpeed, 0);
        }
        else if (transform.position.x > player.position.x)
        {
            rb.velocity = new Vector2(-moveSpeed, 0);
        }
    }
}

things work as expected and the enemy tracks the player across the X axis. So I thought the right thing to do here was to add another void called ChasePlayerVertical and do the same, like this:

{
   [SerializeField]
    Transform player;

   public float agroRange;
   public float moveSpeed;

    Rigidbody2D rb;



    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();

    }

    // Update is called once per frame
    void Update()
    {

        float distToPlayer = Vector2.Distance(transform.position, player.position);
       

        if(distToPlayer < agroRange)
        {
            ChasePlayerHorizontal();
        }
        if(distToPlayer < agroRange)
        {
            ChasePlayerVertical();
        }
    }

    private void ChasePlayerHorizontal()
    {
        if(transform.position.x < player.position.x)
        {
            rb.velocity = new Vector2(moveSpeed, 0);
        }
        else if (transform.position.x > player.position.x)
        {
            rb.velocity = new Vector2(-moveSpeed, 0);
        }
    }


    private void ChasePlayerVertical()
    {
        if (transform.position.y < player.position.y)
        {
            rb.velocity = new Vector2(0, moveSpeed);
        }
        else if (transform.position.y > player.position.y)
        {
            rb.velocity = new Vector2(0, -moveSpeed);
        }
    }
}

but for some reason this ONLY tracks the player on the Y axis. Does anyone know why this is and how I can get this to track the player on both the X and Y axis?

very simple, when you write rb.velocity = new Vector2(0, moveSpeed);

the 0 overwrites the past x

do it like this:

 private void ChasePlayerHorizontal()
    {
        if(transform.position.x < player.position.x)
        {
            rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
        }
        else if (transform.position.x > player.position.x)
        {
            rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
        }
    }


    private void ChasePlayerVertical()
    {
        if (transform.position.y < player.position.y)
        {
            rb.velocity = new Vector2(rb.velocity.x, moveSpeed);
        }
        else if (transform.position.y > player.position.y)
        {
            rb.velocity = new Vector2(rb.velocity.x, -moveSpeed);
        }
    }
2 Likes

Do you know that setting the velocity (like anything else you set) overwrites what’s there already? It should be obvious that setting the velocity (just a Vector2) to one value then setting it to some other value overwrites the first value you set. You set the horizontal velocity then set the vertical; you’re overwriting the horizontal.

You need to calculate the velocity and then just set it in one go. Note you should always refer to the Rigidbody2D position (not the Transform).

    private void ChasePlayerHorizontal()
    {
       var velocity = rb.velocity;

        if(rb.position.x < player.position.x)
        {
            velocity.x = moveSpeed;
        }
        else if (rb.position.x > player.position.x)
        {
            velocity.x = -moveSpeed;
        }

        rb.velocity = velocity;
    }

Thank you very much, this solved my issue and I understand why it’s happening now.