I am having trouble with rotation.

I am making a top-down game and I have it so enemies are chasing the player, but I don’t know how to get them to rotate towards the player. I have tired look at but that caused them to teleport into the player. I have tried looking for tutorials but so far I have not found anything. If you could help me that would be great.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{
public int Health = 100;
public ParticleSystem PS;
public Transform enemypos;
public Rigidbody2D rb;
private bool knockfromright;
public float speed;
private Transform target;

    public void Hit(int damage){
    knockfromright = true;
    Health -= damage;
        if(Health <= 0){
        die();
        }
    }
    void die(){
    Instantiate(PS, enemypos.position, enemypos.rotation);
    Destroy(gameObject);
    }
    void Start(){
    knockfromright = false;
    target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
    }
    void FixedUpdate()
    {
        transform.position = Vector2.MoveTowards(transform.position,target.position,speed * Time.fixedDeltaTime);
        LookAt();
    }
    public void LookAt(){
        Vector3 relativePos = target.rotation;
        Quaternion newRotation = relativePos;
        transform.rotation = Quaternion.Euler(0,0,Mathf.Atan2(newRotation.y - transform.rotation.y, newRotation.x - transform.position.x) * Mathf.Rad2Deg - 90);
    }

@Elbowbread

You are on the right tracks.

As 2D is on flat xy-plane, so you can’t use Unity’s lookAt which would work in 3D, which is a bummer, so you’ll have to somehow calculate the rotation yourself.

First get the direction towards target, with vectors it goes like this:

Vector3 directionToTarget = (targetTra.position - transform.position).normalized;

Then you can convert it to angle value, just feed it as is into your Atan2, and you get a angle value.

Finally use this value as z-rotation value in your Quaternion.Euler.

Note, some Unity rotation values are in radians, so you’ll have to convert those to degrees, in cases where you need values in degrees (like in this case for Quaternion.Euler).

Damn @eses beat me to it.

Just to add Mathf.Atan2 will be in radians and the rotation needs to be in degrees so you need convert it by doing angle * Mathf.Rad2Deg.

Edit: you said that too :smile:

I thought I implemented it but now the Enemys will start off facing but then as they approach, they will do a 180 turn but continue in the same direction. I really don’t know what I am doing.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{
public int Health = 100;
public ParticleSystem PS;
public Transform enemypos;
public Rigidbody2D rb;
private bool knockfromright;
public float speed;
private Transform target;

    public void Hit(int damage){
    knockfromright = true;
    Health -= damage;
        if(Health <= 0){
        die();
        }
    }
    void die(){
    Instantiate(PS, enemypos.position, enemypos.rotation);
    Destroy(gameObject);
    }
    void Start(){
    knockfromright = false;
    target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
    }
    void FixedUpdate()
    {
        transform.position = Vector2.MoveTowards(transform.position,target.position,speed * Time.fixedDeltaTime);
        LookAt();
    }
    public void LookAt(){
        Vector3 directionToTarget = (target.transform.position - transform.position).normalized;
        transform.rotation = Quaternion.Euler(0,0,Mathf.Atan2(directionToTarget.y - transform.rotation.y, directionToTarget.x - transform.position.x) * Mathf.Rad2Deg - 90);
    } 
}

This is how I implemented it.


This is what happens.