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

How to change player animation based on mouse position?

Discussion in '2D' started by demons0fate, Aug 1, 2018.

  1. demons0fate

    demons0fate

    Joined:
    Jun 25, 2018
    Posts:
    2
    I'm wanted to attempt to make a game like "ENTER THE GUNGEON", but I am having trouble with changing the character's animation based on mouse position. If you don't know what I'm talking about, look at GIF. Watch the player as he moves about, changing sprites dependent on where he is shooting, or where the mouse is.

    I have blend tree and code, but it only works when i press the keys. I dont know, how to add mouse position. Help me please!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour
    6. {
    7.     public float moveSpeed = 3f;
    8.  
    9.     Animator thisAnim;
    10.     float lastx, lasty;
    11.  
    12.  
    13.     void Start ()
    14.     {
    15.         thisAnim = GetComponent<Animator>();
    16.     }
    17.    
    18.  
    19.     void Update ()
    20.     {
    21.         Move();
    22.     }
    23.  
    24.  
    25.     void Move()
    26.     {
    27.         Vector3 rightMovement = Vector3.right * moveSpeed * Time.deltaTime * Input.GetAxis("Horizontal");
    28.         Vector3 upMovement = Vector3.up * moveSpeed * Time.deltaTime * Input.GetAxis("Vertical");
    29.         Vector3 heading = Vector3.Normalize(rightMovement + upMovement);
    30.  
    31.  
    32.         transform.position += rightMovement;
    33.         transform.position += upMovement;
    34.  
    35.         UpdateAnimation(heading);
    36.     }
    37.  
    38.     void UpdateAnimation(Vector3 dir)
    39.     {
    40.         if(dir.x == 0f && dir.y == 0f)
    41.         {
    42.             thisAnim.SetFloat("LastDirX", lastx);
    43.             thisAnim.SetFloat("LastDirY", lasty);
    44.             thisAnim.SetBool("Movement", false);
    45.         }
    46.         else
    47.         {
    48.             lastx = dir.x;
    49.             lasty = dir.y;
    50.             thisAnim.SetBool("Movement", true);
    51.         }
    52.  
    53.         thisAnim.SetFloat("DirX", dir.x);
    54.         thisAnim.SetFloat("DirY", dir.y);
    55.  
    56.     }
    57.  
    58.  
    59. }
     

    Attached Files:

  2. Mystic_Quest

    Mystic_Quest

    Joined:
    Feb 22, 2016
    Posts:
    47
    What I did was...

    get my mouse position in update:
    Code (CSharp):
    1. mousepos = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    and used it with my animator:
    Code (CSharp):
    1.  
    2.   if (isAttacking == true)
    3.         {
    4.             anim.SetFloat("Input_x", mousepos.x);
    5.             anim.SetFloat("Input_y", mousepos.y);
    6.         }
    7.  
    Good luck!
     
  3. demons0fate

    demons0fate

    Joined:
    Jun 25, 2018
    Posts:
    2
    Thanks, but i do not understand how to use it ... explain more in detail please