Forum begins after the advertisement:


[Part 1] Jumping animation in a constant loop

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 1] Jumping animation in a constant loop

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #15378
    babun443
    Participant
    Helpful?
    Up
    0
    ::

    Got a problem where the jumping animation is in a loop even if Loop Time is unchecked.

    Unity 2022.3.38f1

    Here’s a video showing the problem:

    View post on imgur.com

    #15383
    babun443
    Participant
    Helpful?
    Up
    0
    ::
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerController : MonoBehaviour
    {
        [Header("Horizontal Movement Settings:")]
        [SerializeField] private float walkSpeed = 1;
    
        [Header("Ground Check Settings:")]
        [SerializeField] private float jumpForce = 45f;
        [SerializeField] private Transform groundCheckPoint;
        [SerializeField] private float groundCheckY = 0.2f;
        [SerializeField] private float groundCheckX = 0.5f;
        [SerializeField] private LayerMask whatIsGround;
    
    
        private Rigidbody2D rb;
        private float xAxis;
        Animator anim;
    
    
        public static PlayerController Instance;
    
        private void Awake()
        {
            if (Instance != null && Instance != this)
            {
                Destroy(gameObject);
            }
            else
            {
                Instance = this;
            }
        }
    
    
        // Start is called before the first frame update
        void Start()
        {
            rb = GetComponent<Rigidbody2D>();
    
            anim = GetComponent<Animator>();
        }
    
        // Update is called once per frame
        void Update()
        {
            GetInputs();
            Move();
            Jump();
            Flip();
        }
    
        void GetInputs()
        {
            xAxis = Input.GetAxisRaw("Horizontal");
        }
    
        void Flip()
        {
            if (xAxis < 0)
            {
                transform.localScale = new Vector2(-1, transform.localScale.y);
            }
            else if (xAxis > 0)
            {
                transform.localScale = new Vector2(1, transform.localScale.y);
            }
        }
    
        private void Move()
        {
            rb.velocity = new Vector2(walkSpeed * xAxis, rb.velocity.y);
            anim.SetBool("Walking", rb.velocity.x != 0 && Grounded());
        }
    
        public bool Grounded()
        {
            if (Physics2D.Raycast(groundCheckPoint.position, Vector2.down, groundCheckY, whatIsGround)
                || Physics2D.Raycast(groundCheckPoint.position + new Vector3(groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround)
                || Physics2D.Raycast(groundCheckPoint.position + new Vector3(-groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    
        void Jump()
        {
            if (Input.GetButtonUp("Jump") && rb.velocity.y > 0)
            {
                rb.velocity = new Vector2(rb.velocity.x, 0);
            }
    
            if (Input.GetButtonDown("Jump") && Grounded())
            {
                rb.velocity = new Vector3(rb.velocity.x, jumpForce);
            }
    
            anim.SetBool("Jumping", !Grounded());
        }
    }
    #15384
    Joseph Tang
    Moderator
    Helpful?
    Up
    0
    ::

    Your code seems fine, but i would like to see how your animator is working. Could you send another video with your Game view set to Unfocused so that it isn’t full screened, then have your animator opened up on the bottom window of your screen?

    I would think that your animation state transition arrows might not be set correctly, so could you click on those as well? Particularly the arrows from Jump > Idle might not have a parameter set to it yet.


    I would presume that once your jump animation has completed [3-4 frames], it will automatically swap to idle since there is no parameter for the transition arrow, and play the idle animation [another 3-4 frames] before trigger the transition back to jumping due to the bool.

    #15387
    babun443
    Participant
    #15389
    Joseph Tang
    Moderator
    Helpful?
    Up
    0
    ::

    Sorry, but you have to reselect the animator by selecting the player in game to see the transitioning happening in real time.

    However, regardless, there doesn’t appear to be any issue with the animation transition arrows.

    The next best thing is to try to reset the assets by duplicating them.
    It’s a simple thing that usually fixes some issues, despite it theoretically doing nothing, but it is a good try to see if it helps anyways.

    Go to your jump animation clip and duplicate it before deleting the original animation clip, then set the loop time off again.
    Go ahead and also delete the animator component and player controller component from the player. This fix sometimes works for some of our other viewers for unknown reasons.

    Otherwise, i have a hard time figuring out what else could be the issue.

    #15391
    babun443
    Participant
    Helpful?
    Up
    0
    ::

    I tried resetting the animation clip and at first I fixed the problem but I accidentally closed Unity.
    The second time I tried resetting the animation clip my PC crashed(got blue screen of death) and the third time I tried resetting the animation clip I couldn’t for some reason so I just decided to use the project files you can download from the first part. I’ll continue through the series without using the downloadable project files.

    Thanks for the help even if I couldn’t fix the problem myself.

    #15393
    Joseph Tang
    Moderator
    Helpful?
    Up
    0
    ::

    Glad to hear you downloaded the project files, and hopefully encountered no further issues. Sometimes these bugs are a bit unconventional so no worries on asking for assistance with them.

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: