SHOW CASE
SHOW CASE

Reputation: 39

Unity 2D rotate the AI enemy to look at player

I'm making a 2D game with two sides, left and right, so the player can only move to the left or right or jump. I made an AI as the enemy. The AI is working fine. He can go to the player to kill him.

The problem is the enemy can't rotate or face the player when he goes to kill him. I want the AI to look at the player. When the player is to the left of the enemy, the enemy should rotate to the left to look at the player. I searched many websites and I didn't get any correct solution. This is my enemy script:

public class enemy : MonoBehaviour
{
    public float speed;
    private Animator animvar;
    private Transform target;
    public GameObject effect;
    public float distance;
    private bool movingRight = true;
    public Transform groundedDetection;
    public float moveInput;
    public bool facingRight = true;

    void Start()
    {
        animvar = GetComponent<Animator>();
        target = GameObject.FindGameObjectWithTag("Player").transform;
    }

    // Update is called once per frame
    void Update()
    {
        if (Vector3.Distance(target.position, transform.position) < 20)
        {
            transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
        }
        else
        {
            transform.Translate(Vector2.right * speed * Time.deltaTime);
        }
        RaycastHit2D groundInfo = Physics2D.Raycast(groundedDetection.position, Vector2.down, distance);
    }

    void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.tag.Equals("danger"))
        {
            Instantiate(effect, transform.position, Quaternion.identity);
            Destroy(gameObject);
        }
        if (col.gameObject.tag.Equals("Player"))
        {
            Instantiate(effect, transform.position, Quaternion.identity);
            Destroy(gameObject);
        }
    }
}

The game is 2D, left, right, with jump using Unity3D.

Upvotes: 1

Views: 6015

Answers (2)

Ger Mc
Ger Mc

Reputation: 640

Its simple enough, just get the Player x position and compare to Enemy's x position, then flip the enemy sprite accordingly

This is my Update() method in my Enemy script. This handles enemy movement and changing direction the sprite is facing:

if (moveRight )
        {
            transform.Translate(2 * Time.deltaTime * moveSpeed, 0, 0);
            transform.localScale = new Vector2(6, 6); //6,6 is just a size that suits my sprite
            
        }

        else if (!moveRight)
        {
            transform.Translate(-2 * Time.deltaTime * moveSpeed, 0, 0);
            transform.localScale = new Vector2(-6, 6);
            
        }

This is my enemy Attack() method , I compare the Player X position to Enemy X position. If Player X is less( to the left of the Enemy), I flip the Enemy sprite to negative

            if (transform.position.x > Player.position.x)
            {
                transform.localScale = new Vector2(-6, 6);
            }
            else if (transform.position.x < Player.position.x)
            {
                transform.localScale = new Vector2(6, 6);
            }

Upvotes: 0

Jimmar
Jimmar

Reputation: 4449

You need a function that would flip your sprite, you can do that by changing the scale of the transform, and keep a boolean to check where it's facing bool facingRight;, so something like this

void Flip(){
      Vector3 scale = transform.localScale;
      scale.x *= -1;
      transform.localScale = scale;
      facingRight = !facingRight;
}

and in your Update check if it needs to be flipped or not

if (Vector3.Distance(target.position,transform.position)<20)
    {

     transform.position=Vector2.MoveTowards(transform.position, target.position,speed*Time.deltaTime);
     if(target.position.x > transform.position.x && !facingRight) //if the target is to the right of enemy and the enemy is not facing right
        Flip();
     if(target.position.x < transform.position.x && facingRight)
        Flip();
   }

Upvotes: 2

Related Questions