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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Player animation based on mouse position

Discussion in '2D' started by simonezingaro69, Jul 29, 2022.

  1. simonezingaro69

    simonezingaro69

    Joined:
    Jul 19, 2022
    Posts:
    4
    I'm new to unity and I can't figure out how to make the player animation based on mouse position.

    This is what I've till now, basically i can move and my 2D character face right or left base on my keys pressed. Is there anyway to make it face right or left based on mouse position??

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class player2 : MonoBehaviour
    6. {
    7.     public float moveSpeed = 5f;
    8.  
    9.     public Rigidbody2D rb;
    10.  
    11.     Vector2 movement;
    12.     void Update()
    13.     {
    14.         movement.x = Input.GetAxisRaw("Horizontal");
    15.         movement.y = Input.GetAxisRaw("Vertical");
    16.  
    17.         movement = new Vector2(movement.x, movement.y).normalized;
    18.    
    19.       if(movement.x > 0)
    20.            transform.localScale = Vector3.one;
    21.            else if (movement.x < 0)
    22.               transform.localScale = new Vector3(-1, 1, 1);
    23.  
    24.     }
    25.  
    26.     void FixedUpdate()
    27.     {
    28.         rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    29.     }
    30. }
    31.  
     
    Last edited: Jul 29, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,945
    Great! Bust out tutorials, lots of tutorials for the kind of game you want to do. When you do tutorials, make sure you pay attention to the steps below or you are wasting your time.

    Otherwise, nobody here can read your mind, so you need to bring a LOT more information to the table.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    You may edit your post above.

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!

    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
     
  3. simonezingaro69

    simonezingaro69

    Joined:
    Jul 19, 2022
    Posts:
    4
    Now i've tried to do this but not working either... The players adpat to the animation based on the keys but not thanks to the mouse position.

    Code (CSharp):
    1.  using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class player2 : MonoBehaviour
    5. {
    6.      public float moveSpeed = 3f;
    7.      public Rigidbody2D rb;
    8.      public Camera cam;
    9.      public Animator animator;
    10.      Vector2 movement;
    11.      Vector2 mousePos;
    12.      Vector2 posDif;
    13.      void Update()
    14.      {
    15.          movement.x = Input.GetAxisRaw("Horizontal");
    16.          movement.y = Input.GetAxisRaw("Vertical");
    17.          mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
    18.          posDif = mousePos - rb.position;
    19.          animator.SetFloat("Horizontal", posDif.x);
    20.          animator.SetFloat("Vertical", posDif.y);
    21.          animator.SetFloat("Horizontal", movement.x);
    22.          animator.SetFloat("Vertical", movement.y);
    23.          animator.SetFloat("Speed", movement.sqrMagnitude);
    24.      }
    25.      void FixedUpdate()
    26.      {
    27.          rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    28.      }
    29. }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,945
    "Not working" is "not helpful."

    I'll post it again for you. It's a checklist. Go through it and remember, we can't see your screen AND we cannot read your mind, so if you want us to know something in order to help you, YOU must communicate that to us clearly.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    You may edit your post above.
     
  5. simonezingaro69

    simonezingaro69

    Joined:
    Jul 19, 2022
    Posts:
    4
    - what you want: Player face right or left based on mouse position

    - what you tried: Code posted before

    - what you expected to happen: Player face right or left based on mouse position

    - what actually happened, especially any errors you see: I see no errors it just wont work, I'm trying to collect as much information as i can from tutorials ( because theres no tutorial that solve my specific problem), i tryng to learn unity these days...

    - links to documentation:
    1: https://forum.unity.com/threads/how-to-change-player-animation-based-on-mouse-position.543336/
    2: https://www.reddit.com/r/Unity2D/comments/93gib7/questionhow_to_change_player_animation_based_on/
    3: https://answers.unity.com/questions/1688706/how-can-i-change-player-sprite-based-on-mouse-posi.html
    4: tutorials on youtube regarding animator and the basic walking of the player
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,945
    And you still have not put Debug.Log() lines in to even see if any of the code is even running? See my first response, tell me what lines are executing when. That is how I know you will be serious about actually figuring out what is happening here.