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

Bug Jumping Animation Bug

Discussion in 'Scripting' started by choptop84, Sep 6, 2021.

  1. choptop84

    choptop84

    Joined:
    Aug 7, 2021
    Posts:
    9

    So I have posted this in the Animation tab before, I am having a bug with my jumping animation, it works in the first level but after that the animation is having the bug... I have checked the script and there is no Animation.play() Involved...

    The Code for the movement and animation is:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour    {
    6.     public CharacterController2D controller;
    7.     public Animator animator;
    8.  
    9.     public float runSpeed = 40f;
    10.  
    11.     float horizontalMove = 0f;
    12.         bool jump = false;
    13.     bool crouch = false;
    14.  
    15.      
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.      
    20.     horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
    21.  
    22.     animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
    23.  
    24.     if (Input.GetButtonDown("Jump"))
    25.     {
    26.         jump = true;
    27.         animator.SetBool("IsJumping", true);
    28.     }
    29.  
    30.     if (Input.GetButtonDown("Crouch"))
    31.     {
    32.         crouch = true;
    33.     } else if (Input.GetButtonUp("Crouch"))
    34.  
    35.     {
    36.         crouch = false;
    37.     }
    38.  
    39.  
    40.  
    41.  
    42.     }
    43.  
    44.     public void OnLanding ()
    45.     {
    46.         animator.SetBool("IsJumping", false);
    47.     }
    48.  
    49.     public void OnCrouching (bool IsCrouching)
    50.     {
    51.     animator.SetBool("IsCrouching", IsCrouching);
    52.     }
    53.  
    54.    
    55.  
    56.  
    57.     void FixedUpdate ()
    58.     {
    59.             // Move our character
    60.         controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
    61.         jump = false;
    62.      
    63.     }
    64. }
    I also checked the other script that handles movement speed but there is no reference to animator or animation in it.

    (To describe it more in detail, after the first level [Scene 1] The animation is having trouble detecting the IsJumping Bool that I have set up.)
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Hey,

    It's very difficult to tell what's wrong, especially because your character controller logic is broken up in so many different places. The first thing that comes to mind is that you may have forgotten to set up ground tags/layers on your other levels. Although, even that is difficult to tell because your OnLanding() method is being called from somewhere else.

    To keep things simple and easy to debug, you should probably keep all of your movement logic in one place. You're using sprites, so you don't need animation transitions at all. Using
    Animator.Play()
    will be much simpler and cost you nothing.
     
    KalOBrien likes this.
  3. KalOBrien

    KalOBrien

    Administrator

    Joined:
    Apr 20, 2021
    Posts:
    89
    Moved to Scripting, folks here would be better able to help with the issue.