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

Help: Player stops moving, but keeps holding the run key, the player direction always faces north

Discussion in 'Animation' started by Hommet_, Jan 7, 2021.

  1. Hommet_

    Hommet_

    Joined:
    Jul 14, 2015
    Posts:
    1
    Hard to come up with the right title for this one. I'm using animator.Play to directly call animations for my 2d top down game. When the player holds the shift key while moving it transitions them from walking to running. Everything is working perfectly except for a single odd behavior. If the player lets go of the direction key(s) but keeps holding shift the player direction always snaps north for the idle animation no matter which direction they were moving when they stopped. Can anyone see where I screwed up? Beginner coder, so anything helps.Thanks!!


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement2 : MonoBehaviour
    6. {
    7.     Rigidbody2D body;
    8.     Vector2 movement;
    9.  
    10.     public Animator animator;
    11.  
    12.     float horizontal;
    13.     float vertical;
    14.     float moveLimiter = 0.7f;
    15.  
    16.     public float runSpeed = 1.0f;
    17.     public float walkSpeed = 1.0f;
    18.     public bool Running = false;
    19.     public bool Walking = false;
    20.  
    21.     void Start()
    22.     {
    23.         body = GetComponent<Rigidbody2D>();
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.  
    29.         // Gives a value between -1 and 1
    30.         horizontal = Input.GetAxisRaw("Horizontal"); // -1 is left
    31.         vertical = Input.GetAxisRaw("Vertical"); // -1 is down
    32.         // handles animation
    33.         movement.x = Input.GetAxisRaw("Horizontal");
    34.         movement.y = Input.GetAxisRaw("Vertical");
    35.  
    36.         // play walk animation
    37.         if (movement != Vector2.zero && Running == false)
    38.         {
    39.             animator.Play("Walk");
    40.             animator.SetFloat("Horizontal", movement.x);
    41.             animator.SetFloat("Vertical", movement.y);
    42.         }
    43.         // play run animation
    44.         if (Running)
    45.         {
    46.             animator.Play("Run");
    47.             animator.SetFloat("Horizontal", movement.x);
    48.             animator.SetFloat("Vertical", movement.y);
    49.         }
    50.         // play idle animation
    51.         if (movement == Vector2.zero)
    52.         {
    53.             animator.Play("Idle");
    54.         }
    55.  
    56.         animator.SetFloat("Speed", movement.sqrMagnitude);
    57.         // end animation handler
    58.     }
    59.  
    60.     void FixedUpdate()
    61.     {
    62.         if (horizontal != 0 && vertical != 0) // Check for diagonal movement
    63.         {
    64.             // limit movement speed diagonally, so you move at 70% speed
    65.             horizontal *= moveLimiter;
    66.             vertical *= moveLimiter;
    67.         }
    68.         if (vertical != 0) // Check for vertical movement
    69.         {
    70.             // limit movement speed vertically, so you move at 70% speed (for better topdown angle effect)
    71.             vertical *= moveLimiter;
    72.         }
    73.         // determine what is running
    74.         if (movement != Vector2.zero && Input.GetKey("left shift") || movement != Vector2.zero && Input.GetKey("right shift"))
    75.         {
    76.             Running = true;
    77.             body.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed);
    78.         }
    79.  
    80.         // determine what is walking
    81.         else
    82.         {
    83.             Running = false;
    84.             body.velocity = new Vector2(horizontal * walkSpeed, vertical * walkSpeed);
    85.         }
    86.     }
    87.  
    88. }
    89.  
    90.