Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Changing player sprite with mouse position.

Discussion in '2D' started by Madgamer0307, May 7, 2022.

  1. Madgamer0307

    Madgamer0307

    Joined:
    Apr 27, 2020
    Posts:
    6
    I'm making a 2d game. I would like the player to change it's sprite when the mouse is at a different direction.

    Here is the code I have now.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class playerMovement : MonoBehaviour {
    6.  
    7.      public Rigidbody2D rb;
    8.      public float Speed;
    9.      public Animator anim;
    10.  
    11.      Vector2 move;
    12.  
    13.      void Update() {
    14.  
    15.          move.x = Input.GetAxisRaw("Horizontal");
    16.          move.y = Input.GetAxisRaw("Vertical");
    17.  
    18.          if(move.x >= 0.01 || move.x <= -0.01 || move.y >= 0.01 || move.y <= -0.01)
    19.          {
    20.              anim.SetBool("moving", true);
    21.          }else{
    22.              anim.SetBool("moving", false);
    23.          }
    24.      }
    25.  
    26.      void FixedUpdate() {
    27.          rb.MovePosition (rb.position += move * Speed * Time.fixedDeltaTime);
    28.      }
    29.  
    30. }
    31.  
    I have 3 Animator parameters. Floats xDir, yDir, and bool moving.

    Any help would be highly appreciated.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Millions of example code to change a sprite.

    You need to decide if YOU are doing this sprite change, or if you are going to have the Animation system do it for you. I see animation property setter calls but only you know how it is set up.

    Mice don't have direction.

    Do you mean "different relative direction from player?"

    You can subtract two vectors to find their relative difference

    From that you can decide what to do with the result.
     
  3. Madgamer0307

    Madgamer0307

    Joined:
    Apr 27, 2020
    Posts:
    6
    I have 4 animations for 4 different directions of walking, and have set it up with animation parameters. I just need a way to change the parameter amount based on the position of mouse in relation to the player.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    I'll say it again:

    - subtract the player position from the mouse position to get an offset vector

    - analyze that vector to decide direction:

    ---> if it is more vertically displaced than horizontal, you know it is up / down

    ---> else it is left / right

    ---> now check the sign of the appropriate axis to decide up/down/left/right

    - finally set the parameters for your animation.

    Seriously though, just work through some actual tutorials with clear visuals. Don't limit yourself to the text characters you can find on a forum. This is extremely well-covered well-understood Unity tech.
     
  5. Madgamer0307

    Madgamer0307

    Joined:
    Apr 27, 2020
    Posts:
    6
    Thank you. Yes tutorials are good, but a challenge is good too.