Sprite change to up, down, left and right depending on target location

Hello, I have a player object which I want to change it’s animation to Up if it’s target is above it Right if it’s target is beside it, and down if it’s target is below it. My player moves towards an object which follows the mouse. Here’s the code I tried where 1 is up 0 is right/let and -1 is down.

if ((mouseAim.position - transform.position).normalized.x > (mouseAim.position - transform.position).normalized.y)
        {
            animator.SetInteger("Walk", 0);
        }
        else
        {
            if (mouseAim.position.y > transform.position.y)
            {
                animator.SetInteger("Walk", 1);
            }
            else
            {
                animator.SetInteger("Walk", -1);
            }
        }

This tutorial should get you started.

It uses the keys but it should give you enough of an idea to do it with the mouse position too, let me see if I can find something more specific though.

Yes, the animator isn’t really the problem, It’s my code. If my target is on the left of my gameobject the values are negative, and If it’s on the right the values are positive, I’ve tried it normalized and not normalized. So sometimes the Y is the opposite of the X (positive and negative wise) and other times they are the same (positive and negative wise) which makes my code (this line) if ((mouseAim.position - transform.position).normalized.x > (mouseAim.position - transform.position).normalized.y) useless. Is there another way of calculating this? Thanks :slight_smile:

I think you’ll have to wait for someone more experienced to come along I’m afraid, I’m sure it can be done though, it’s just a matter of knowing the right maths.

But what if it’s both above and to the right?

To find out if it’s to the right or the left, just check if mouseAim.position.x > transform.position.x, and to check if it’s above or below use mouseAim.position.y > transform.position.y.

Yes that’s why I was calculating the distance between the Y and the X to know which one was greater but it would go into the negatives. So if the mouseAim.x is greater than transform.position.x and mouseAim.y is greater than transform.position.y, how do I make my object know which sprite to change to? (sprite Right or sprite Up), please help :slight_smile:

So you want the move up sprite to show if it’s more up than to the right? And the right sprite if it’s more to the right than up?

Something like this should work

float deltaX = mouseAim.position.x - transform.position.x;
float deltaY = mouseAim.position.y - transform.position.y;

if ( !(deltaX == 0f && deltaY == 0f) ) // No change if exactly on mouse position
{
    if (deltaX > 0f && Mathf.Abs(deltaX) > Mathf.Abs(deltaY))
    {
        ChangeToMoveRightSprite();
    }
    else if (deltaX < 0f && Mathf.Abs(deltaX) > Mathf.Abs(deltaY))
    {
        ChangeToMoveLeftSprite();
    }
    else if (deltaY > 0f && Mathf.Abs(deltaY) > Mathf.Abs(deltaX))
    {
        ChangeToMoveUpSprite();
    }
    else if (deltaY < 0f && Mathf.Abs(deltaY) > Mathf.Abs(deltaX))
    {
        ChangeToMoveDownSprite();
    }
    else
    {
        Debug.Log("Somethings wrong");
    }
}

2D blend trees could work also,

Ok, thank you guys so much! It’s working! :slight_smile: