U
U
UnityNewPlayer2020-08-04 17:08:30
Unity
UnityNewPlayer, 2020-08-04 17:08:30

How to turn an enemy towards the player in Unity2D?

I'm making a game on Unity2D with a top view, where you need to destroy the enemy base. Various robots (enemies) should interfere with me, but I stalled on creating AI. I did a patrol but couldn't implement the pursuit. I want the enemy to turn towards the player and walk forward and I don't know how to do it. Tried through LookAt, but the turn is too sharp. If through Qutarnion.Lerp, then the enemy is constantly rotated to the 0th value in Z, or rotated, but in all coordinates, although only Z is needed. So how do I rotate the enemy towards the player? Here is the code, do not judge strictly because I'm just starting.

public class EnemyAI : MonoBehaviour {

  public float speed;
  public float mSpeed;
  private float waitTime;
  public float StartWaitTime;

  float angle;
  public float value;
  public float mvalue;

  public Transform player;

  void Start()
  {
    waitTime = StartWaitTime;
  }


  	void Update() {

    if (waitTime <= 0)
    {
      value = Random.Range(0, 10);   
      mvalue = Random.Range(1, 4);   
      waitTime = StartWaitTime;   
 			RandomAngle();   
    }
 		else
    {
 			waitTime -= Time.deltaTime; //таймер
    }
  		if(Vector2.Distance(transform.position, player.transform.position) < 10f)
    {
      Quaternion Rot2 = Quaternion.Euler(0, 0, player.position.z);

      transform.rotation = Quaternion.Lerp(transform.rotation, Rot2, Time.deltaTime * speed); 
    }
 		else if (mvalue == 2)
    {
      transform.Translate(Vector2.left * mSpeed * Time.deltaTime);
    }
    else
    {
      Quaternion Rot = Quaternion.Euler(0, 0, angle);

      transform.rotation = Quaternion.Lerp(transform.rotation, Rot, Time.deltaTime * speed);
    }
  }

  void RandomAngle()
  {
    if (value == 1)
    {
      angle = 45;
    }
    if (value == 2)
    {
      angle = 90;
    }
    if (value == 3)
    {
      angle = -45;
    }
    if (value == 4)
    {
      angle = -90;
    }
    if (value == 5)
    {
      angle = 135;
    }
    if (value == 6)
    {
      angle = 180;
    }
    if (value == 7)
    {
      angle = -135;
    }
    if (value == 8)
    {
      angle = 0;
    }
  }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
privet_andrei123, 2020-08-05
@UnityNewPlayer

Hey ! You need to rotate on the y axis (probably ) try using lookat(your player) also to look up player position you need to use if(distans() player position , tansform.position)
You can also look at this ( https://youtu.be/S7- unUDLI6A ) yes, yes, this is advertising (no)

G
Grish Poghosyan, 2020-08-04
@hovsepyann

I recommend to look at this , suddenly it will help. (not advertising, dear administration)

Z
zZaKko, 2020-08-04
@zZaKko

Here's what I found on google.
unity3d.ru/distribution/viewtopic.php?f=5&t=51710
https://www.cyberforum.ru/unity/thread1589288.html
Everything is ready on the internet, you just need to search a little :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question