itsanantk
itsanantk

Reputation: 264

My sprite only flips when it's close to the player

I'm making an enemy in my 2D Unity game, and I was trying to flip the sprite when it faces the opposite direction, the thing is, the sprite would only flip when the player was very close to the enemy. This is my code:

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

public class EnemyMovement : MonoBehaviour
{

    public float moveSpeed;
    private PlayerMovement player;
    public float playerRange;
    public LayerMask playerLayer;
    public bool playerinRange;
    private bool Right = false;

    private void Start()
    {
        player = FindObjectOfType<PlayerMovement>();
    }

    private void Update()
    {
        playerinRange = Physics2D.OverlapCircle(transform.position, playerRange, playerLayer);

        if (playerinRange)
        {
            transform.position = Vector3.MoveTowards(transform.position, player.transform.position, moveSpeed * Time.deltaTime);
        }

        Flip();
    }

    private void OnDrawGizmosSelected()
    {
        Gizmos.DrawSphere(transform.position, playerRange);
    }

    private void Flip()
    {
        if (transform.position.x > 0 && !Right || transform.position.x < 0 && Right)
        {
            Right = !Right;
            Vector3 theScale = transform.localScale;

            theScale.x *= -1;

            transform.localScale = theScale;
        }
    }
}

Upvotes: 1

Views: 57

Answers (1)

Vale
Vale

Reputation: 396

Looking at your code, what's happening is that the direction the enemy is facing is dependent of the position of the enemy in the world (since you are using the x component of transform.position). If your enemy x position is positive, the Right boolean will be true, otherwise it will be false (also the 0 case isn't accounted for).

Since the only thing that can move the enemy in this code snippet is the Vector3.MoveToward when the player is in range, I suspect what's happening is that as your player gets close, the enemy moves and the x position value goes from negative to positive (or the opposite), flipping the sprite.

Now, linking the sprite direction to the enemy's position isn't something you want to do. Instead, you want the enemy to face the direction it is moving toward. This can be done by having a velocity parameter that you use to move your enemy and which will tell you in which direction he is moving (and at which speed) for instance. If you want to keep the move toward, then you need to figure out in which direction it is making you move and flip the sprite accordingly. Here is a code example base on your code:

private void Update()
{
    playerinRange = Physics2D.OverlapCircle(transform.position, playerRange, playerLayer);

    if (playerinRange)
    {
        transform.position = Vector3.MoveTowards(transform.position, player.transform.position, moveSpeed * Time.deltaTime);

        Flip(player.transform.position.x - transform.position.x);
    }
}

private void Flip(float direction)
{
    Right = (direction >= 0);

    Vector3 theScale = transform.localScale;
    theScale.x = Mathf.Abs(theScale.x) * Mathf.Sign(direction);
    transform.localScale = theScale;
}

Upvotes: 2

Related Questions