0
\$\begingroup\$
 void Update()
{
    ChasingPlayer();
}

void ChasingPlayer()
{
    if (chasing == true)
    {
        transform.LookAt(player.transform);  //facing player
        transform.position += transform.forward * approaching * Time.deltaTime;  //going towards player
    }

        float distance = Vector3.Distance(transform.position, player.position);
    Debug.Log(distance); //calculating the distance between player and enemy
    if(distance < 10)
    {
        chasing = false;
        transform.position += Vector3.left * 2f * Time.deltaTime;
        Debug.Log("STOP!");
    }
}

I want my A.I to approach to the player but when it reaches within 10 feet(?), I want it to stop moving towards the player and strafe/move to the left or right from there but instead it just moving in a curve movement that leads towards the player. How should i stop the AI within 10 feet(?) and move to the left/right?

\$\endgroup\$
2
  • \$\begingroup\$ When/how do you set the chasing flag to true? \$\endgroup\$
    – Vaillancourt
    Jul 27, 2020 at 15:21
  • 1
    \$\begingroup\$ Note that an animated gif would help a lot get what's going on :) \$\endgroup\$
    – Vaillancourt
    Jul 27, 2020 at 15:23

1 Answer 1

0
\$\begingroup\$

Your code reads

transform.position += Vector3.left * 2f * Time.deltaTime;

Vector3.left is a world-space direction; it does not take the enemy's current rotation into account. Perhaps what you want is this:

transform.position += transform.right * 2f * Time.deltaTime; //to go right

or

transform.position -= transform.right * 2f * Time.deltaTime; //to go left
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .