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

My Jump Animation is stuck at frame 1 after editing script.

Discussion in 'Animation' started by ScaryFace, Apr 3, 2020.

  1. ScaryFace

    ScaryFace

    Joined:
    Aug 7, 2015
    Posts:
    12
    Hey Guys

    So I edited my character controller script to check for Ground before handling the "if" statements to change the animation parameters. it was working fairly well before but now my 1st jump animation gets stuck at frame one the double jump works as intended..

    Below is my code and screen shots of the transition settings, i don't know...o_O:( any help will be appreciated.

    Also I have a problem where if i quickly press jump the moment the character lands the animations don't play at all, this is true for both the Jump animations.


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerControllerScript : MonoBehaviour
    7. {
    8.     Animator animator;
    9.     Rigidbody2D rb2d;
    10.     SpriteRenderer spriteRenderer;
    11.  
    12.     bool isGrounded;
    13.     private int weaponSwingCount = 0;
    14.     public float velocity = 1f;
    15.     public float jumpVelocity = 5f;
    16.  
    17.     float axisHorizontal = 0f;
    18.     float moveHorizontal = 0f;
    19.  
    20.     [SerializeField]
    21.     Transform groundCheck;
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         animator = GetComponent<Animator>();
    26.         rb2d = GetComponent<Rigidbody2D>();
    27.         spriteRenderer = GetComponent<SpriteRenderer>();
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void FixedUpdate()
    32.     {
    33.         axisHorizontal = Input.GetAxisRaw("Horizontal");//collect axis also used for Flip X Calc
    34.         moveHorizontal = DigitizeAnalogInput(axisHorizontal);
    35.  
    36.         rb2d.velocity = new Vector2(moveHorizontal, rb2d.velocity.y); // change in velocity
    37.         animator.SetFloat("Speed", Mathf.Abs(moveHorizontal)); //set parameter speed to 1 or 0
    38.  
    39.         isGrounded = GroundedCheck(isGrounded); //grounded check
    40.         if (isGrounded)
    41.         {
    42.             if (Input.GetButtonDown("Jump"))//1st Jump
    43.             {
    44.                 rb2d.AddForce(transform.up * jumpVelocity, ForceMode2D.Impulse);
    45.                 animator.SetBool("IsJumping", true);
    46.             }
    47.  
    48.  
    49.             if (Input.GetButtonDown("Fire3") && !(animator.GetBool("IsSwordDrawn")))//Drawing Sword
    50.             {
    51.                 animator.SetBool("IsSwordDrawn", true);
    52.             }
    53.             else if (Input.GetButtonDown("Fire3") && animator.GetBool("IsSwordDrawn"))//Sheathing Sword
    54.             {
    55.                 animator.SetBool("IsSwordDrawn", false);
    56.                 animator.SetInteger("Swing", 0);
    57.             }
    58.  
    59.             if (animator.GetBool("IsSwordDrawn") && rb2d.velocity.x == 0)
    60.             {
    61.                 if (Input.GetButtonDown("Fire1") && weaponSwingCount == 0)//Grounded Attack
    62.                 {
    63.                     ++weaponSwingCount;
    64.                     animator.SetInteger("Swing", 1);
    65.                     animator.SetTrigger("SwingTrigger");
    66.                     Debug.Log(weaponSwingCount);
    67.                 }
    68.                 else if (Input.GetButtonDown("Fire1") && weaponSwingCount == 1)
    69.                 {
    70.                     ++weaponSwingCount;
    71.                     animator.SetInteger("Swing", 2);
    72.                     animator.SetTrigger("SwingTrigger");
    73.                     Debug.Log(weaponSwingCount);
    74.                 }
    75.                 else if (Input.GetButtonDown("Fire1") && weaponSwingCount == 2)
    76.                 {
    77.                     ++weaponSwingCount;
    78.                     animator.SetInteger("Swing", 3);
    79.                     animator.SetTrigger("SwingTrigger");
    80.                     Debug.Log(weaponSwingCount);
    81.                 }
    82.                 else if (weaponSwingCount == 3)
    83.                 {
    84.                     weaponSwingCount = 0;
    85.      
    86.  
    87.                 }
    88.             }
    89.  
    90.         }
    91.         else if(!(isGrounded))
    92.         {
    93.             if (Input.GetButtonDown("Jump") && animator.GetBool("IsJumping"))
    94.             {
    95.                 rb2d.AddForce(transform.up * jumpVelocity, ForceMode2D.Impulse);
    96.                 animator.SetBool("IsJumping", false);
    97.                 animator.SetBool("IsSummersalting", true);
    98.             }
    99.         }
    100.  
    101.               FlipX(spriteRenderer, axisHorizontal);
    102.  
    103.     }    
    104.    
    105.  
    106.     bool GroundedCheck(bool grounded)
    107.     {
    108.         if (Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Player")))
    109.         {
    110.             grounded = true;
    111.             animator.SetBool("IsJumping", false);
    112.             animator.SetBool("IsSummersalting", false);
    113.         }
    114.         else
    115.         {
    116.             grounded = false;
    117.         }
    118.         return grounded;
    119.     }
    120.  
    121.     void FlipX(SpriteRenderer sr, float horizontal)
    122.     {
    123.         if (axisHorizontal < 0)
    124.         {
    125.             spriteRenderer.flipX = true;
    126.         }
    127.         if (axisHorizontal > 0)
    128.         {
    129.             spriteRenderer.flipX = false;
    130.         }
    131.     }
    132.  
    133.     int DigitizeAnalogInput(float axisMovement)//change input from controller to whole numbers
    134.     {
    135.         const float threshhold = 0.7f;
    136.         if (axisMovement > threshhold) return 1;
    137.         if (axisMovement < -threshhold) return -1;
    138.         return 0;
    139.     }
    140.  
    141. }
    upload_2020-4-3_17-43-34.png

    upload_2020-4-3_17-43-51.png
    upload_2020-4-3_17-44-2.png
     
  2. ScaryFace

    ScaryFace

    Joined:
    Aug 7, 2015
    Posts:
    12
    Think I Fixed it, the "can transition to" check box on the any state sub needed to be unchecked.
    unchecked it and my jump animation works fine now.
     
    Etheranda likes this.
  3. Etheranda

    Etheranda

    Joined:
    Jul 28, 2021
    Posts:
    1
    Thank you so much! I have spent a good couple of hours messing around with code, looking on multiple forums and it was as simple as unticking one box! it's always those pesky ticky boxes that seem to break everything, even on Unreal Engine haha.