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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Sprint Animation Only Playing First Sprite

Discussion in '2D' started by TheShadowGamer117, Mar 3, 2018.

  1. TheShadowGamer117

    TheShadowGamer117

    Joined:
    Jan 17, 2018
    Posts:
    10
    I'm having a problem with my sprint animation. I have it set up so that there is a trigger linked to the variable "sprint" in my script, which I have set to be active when shift is held down. While it does activate and speeds up the player sprite, it also only plays the first frame of the sprite animation and then switches back to the idle/walk pose. Can someone help me figure this out?

    I'm probably doing something wrong with the trigger (first time using them with animations) but can't seem to figure out what I'm doing wrong.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour {
    6.  
    7.     private Rigidbody2D myRigidBody;
    8.  
    9.     private Animator myAnimator;
    10.  
    11.     [SerializeField]
    12.     private float movementSpeed;
    13.  
    14.     private float sprintSpeed = 8;
    15.  
    16.     private bool sprint;
    17.  
    18.     private bool facingRight;
    19.  
    20.     // Use this for initialization
    21.     void Start () {
    22.         facingRight = true;
    23.         myRigidBody = GetComponent<Rigidbody2D> ();
    24.         myAnimator = GetComponent<Animator>();
    25.      
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void FixedUpdate () {
    30.  
    31.         float horizontal = Input.GetAxis ("Horizontal");
    32.  
    33.         HandleMovement (horizontal);
    34.  
    35.         Flip (horizontal);
    36.  
    37.         if (Input.GetKey (KeyCode.LeftShift)) {
    38.  
    39.             sprint = true;
    40.  
    41.             movementSpeed = sprintSpeed;
    42.  
    43.             HandleSprint ();
    44.         } else {
    45.             movementSpeed = 4;
    46.             sprint = false;
    47.         }
    48.     }
    49.  
    50.     private void HandleMovement(float horizontal){
    51.  
    52.  
    53.         myRigidBody.velocity = new Vector2 (horizontal * movementSpeed, myRigidBody.velocity.y);
    54.  
    55.         myAnimator.SetFloat ("speed", Mathf.Abs(horizontal));
    56.          
    57.     }
    58.  
    59.     private void HandleSprint(){
    60.  
    61.         if (sprint) {
    62.             myAnimator.SetTrigger ("sprint");
    63.         }
    64.  
    65.     }
    66.  
    67.     private void Flip(float horizontal){
    68.  
    69.         if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight) {
    70.  
    71.             facingRight = !facingRight;
    72.  
    73.             Vector3 theScale = transform.localScale;
    74.  
    75.             theScale.x *= -1;
    76.  
    77.             transform.localScale = theScale;
    78.         }
    79.     }
    80. }
    81.  
     
    Last edited: Mar 3, 2018
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    We probably need to see a screen dump of your Animator setup as well.
     
  3. TheShadowGamer117

    TheShadowGamer117

    Joined:
    Jan 17, 2018
    Posts:
    10
    upload_2018-3-3_9-10-11.png
     
  4. bobthesmartypants

    bobthesmartypants

    Joined:
    Apr 14, 2017
    Posts:
    2
    It would help if you showed what your transition conditions were, but regardless I would suggest making the run condition a bool instead because running is a true/false type of decision, not a trigger.
     
  5. TheShadowGamer117

    TheShadowGamer117

    Joined:
    Jan 17, 2018
    Posts:
    10
    Okay, I set it up as a bool and I also muted the transition into itself (don't think I needed that for the way I was doing it, but friend recommended it) Setting it to a bool seems to have fixed the issue where it doesn't play all the way, but now it won't stop playing after shift is released.

    upload_2018-3-3_17-4-24.png
     
  6. TheShadowGamer117

    TheShadowGamer117

    Joined:
    Jan 17, 2018
    Posts:
    10
    Finally got it. Here's the updated script for a viewers future reference.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour {
    6.  
    7.     private Rigidbody2D myRigidBody;
    8.  
    9.     private Animator myAnimator;
    10.  
    11.     [SerializeField]
    12.     private float movementSpeed;
    13.  
    14.     private float sprintSpeed = 16;
    15.  
    16.     private bool sprint;
    17.  
    18.     private bool facingRight;
    19.  
    20.     // Use this for initialization
    21.     void Start () {
    22.         facingRight = true;
    23.         myRigidBody = GetComponent<Rigidbody2D> ();
    24.         myAnimator = GetComponent<Animator>();
    25.        
    26.     }
    27.    
    28.     // Update is called once per frame
    29.     void FixedUpdate () {
    30.  
    31.         float horizontal = Input.GetAxis ("Horizontal");
    32.  
    33.         HandleMovement (horizontal);
    34.  
    35.         Flip (horizontal);
    36.  
    37.         if (Input.GetKey (KeyCode.LeftShift) && horizontal != 0) {
    38.  
    39.             movementSpeed = sprintSpeed;
    40.  
    41.             myAnimator.SetBool ("sprint", true);
    42.  
    43.             //HandleSprint ();
    44.         } else {
    45.             movementSpeed = 4;
    46.             myAnimator.SetBool ("sprint", false);
    47.         }
    48.        
    49.        
    50.     }
    51.  
    52.     private void HandleMovement(float horizontal){
    53.  
    54.  
    55.         myRigidBody.velocity = new Vector2 (horizontal * movementSpeed, myRigidBody.velocity.y);
    56.    
    57.         myAnimator.SetFloat ("speed", Mathf.Abs(horizontal));
    58.            
    59.     }
    60.  
    61.     private void Flip(float horizontal){
    62.  
    63.         if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight) {
    64.  
    65.             facingRight = !facingRight;
    66.  
    67.             Vector3 theScale = transform.localScale;
    68.  
    69.             theScale.x *= -1;
    70.  
    71.             transform.localScale = theScale;
    72.         }
    73.     }
    74. }
    75.  
     
    theANMATOR2b likes this.