Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by IAMBATMAN, May 8, 2016.

  1. IAMBATMAN

    IAMBATMAN

    Joined:
    Aug 14, 2015
    Posts:
    272
    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.


    Code (CSharp):
    1. if ((mouseAim.position - transform.position).normalized.x > (mouseAim.position - transform.position).normalized.y)
    2.         {
    3.             animator.SetInteger("Walk", 0);
    4.         }
    5.         else
    6.         {
    7.             if (mouseAim.position.y > transform.position.y)
    8.             {
    9.                 animator.SetInteger("Walk", 1);
    10.             }
    11.             else
    12.             {
    13.                 animator.SetInteger("Walk", -1);
    14.             }
    15.         }
     
  2. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    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.
     
  3. IAMBATMAN

    IAMBATMAN

    Joined:
    Aug 14, 2015
    Posts:
    272
    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)
    Code (CSharp):
    1. if ((mouseAim.position - transform.position).normalized.x > (mouseAim.position - transform.position).normalized.y)
    useless. Is there another way of calculating this? Thanks :)
     
  4. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    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.
     
  5. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    967
    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.
     
  6. IAMBATMAN

    IAMBATMAN

    Joined:
    Aug 14, 2015
    Posts:
    272
    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 :)
     
  7. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    967
    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

    Code (csharp):
    1.  
    2. float deltaX = mouseAim.position.x - transform.position.x;
    3. float deltaY = mouseAim.position.y - transform.position.y;
    4.  
    5. if ( !(deltaX == 0f && deltaY == 0f) ) // No change if exactly on mouse position
    6. {
    7.     if (deltaX > 0f && Mathf.Abs(deltaX) > Mathf.Abs(deltaY))
    8.     {
    9.         ChangeToMoveRightSprite();
    10.     }
    11.     else if (deltaX < 0f && Mathf.Abs(deltaX) > Mathf.Abs(deltaY))
    12.     {
    13.         ChangeToMoveLeftSprite();
    14.     }
    15.     else if (deltaY > 0f && Mathf.Abs(deltaY) > Mathf.Abs(deltaX))
    16.     {
    17.         ChangeToMoveUpSprite();
    18.     }
    19.     else if (deltaY < 0f && Mathf.Abs(deltaY) > Mathf.Abs(deltaX))
    20.     {
    21.         ChangeToMoveDownSprite();
    22.     }
    23.     else
    24.     {
    25.         Debug.Log("Somethings wrong");
    26.     }
    27. }
    28.  
     
  8. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,903
  9. IAMBATMAN

    IAMBATMAN

    Joined:
    Aug 14, 2015
    Posts:
    272
    Ok, thank you guys so much! It's working! :)
     
    Last edited: May 10, 2016