Forum begins after the advertisement:


[part 1] my character can’t jump after adding animation

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [part 1] my character can’t jump after adding animation

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #17082
    adam
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    I did everything exactly like in the first video of the tutorial series. Unfortunately, my character assets can´t jump after adding walking and jumping animation and how to prevent jumping checkbox automatically checked in animation->parameter menu . here’s my code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerController : MonoBehaviour
    {
        [Header("Horizontal Movement Setting")]
        [SerializeField] private float walkSpeed = 1;
    
        [Header("Ground Check Setting")]
        [SerializeField] private Transform groundCheckPoint;
        [SerializeField] private float groundCheckY = 0.2f;
        [SerializeField] private float groundCheckX = 0.5f;
        [SerializeField] private LayerMask whatIsGround;
        [SerializeField] private float jumpForce = 45;
    
        private float xAxis;
        private Rigidbody2D rb;
        private bool isGrounded;
        private Animator anim;
    
        // Start is called once before the first execution of Update after the MonoBehaviour is created
        void Start()
        {
            rb = GetComponent<Rigidbody2D>();
            anim = GetComponent<Animator>();
        }
    
        // Update is called once per frame
        void Update()
        {
            GetInputs();
            Move();
            Jump();
            UpdateAnimations();
        }
    
        void GetInputs()
        {
            xAxis = Input.GetAxisRaw("Horizontal");
        }
    
        private void Move()
        {
            rb.linearVelocity = new Vector2(walkSpeed * xAxis, rb.linearVelocity.y);
        }
    
        private void Jump()
        {
            if (Input.GetButtonUp("Jump") && rb.linearVelocity.y > 0)
            {
                rb.linearVelocity = new Vector2(rb.linearVelocity.x, 0);
            }
    
            if (Input.GetButtonDown("Jump") && Grounded())
            {
                Debug.Log("Jump button pressed and player is grounded");
                rb.linearVelocity = new Vector3(rb.linearVelocity.x, jumpForce);
            }
        }
    
        private void UpdateAnimations()
        {
            anim.SetBool("Walking", rb.linearVelocity.x != 0 && Grounded());
            anim.SetBool("Jumping", !Grounded());
        }
    
        public bool Grounded()
        {
            bool groundCheck1 = Physics2D.Raycast(groundCheckPoint.position, Vector2.down, groundCheckY, whatIsGround);
            bool groundCheck2 = Physics2D.Raycast(groundCheckPoint.position + new Vector3(groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround);
            bool groundCheck3 = Physics2D.Raycast(groundCheckPoint.position + new Vector3(-groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround);
    
            return groundCheck1 || groundCheck2 || groundCheck3;
        }
    }
    

    or you can see here https://pastebin.com/17GqTCd8 and videos about my error in unity3d https://streamable.com/oy5i5e

    #17084
    Ser Apustaja
    Level 2
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    It looks like you’re having trouble getting your character to jump in Unity after adding animations. Let’s troubleshoot your code and check a few things that could be causing the issue.

    1. Jump Logic: In your Jump method, you are checking if the player is grounded before allowing the jump. Ensure that the Grounded() method is returning true when the player is on the ground. You can add some debug statements to verify this. For example:

       if (Input.GetButtonDown("Jump") && Grounded())
       {
           Debug.Log("Jump button pressed and player is grounded");
           rb.velocity = new Vector2(rb.velocity.x, jumpForce); // Use rb.velocity instead of rb.linearVelocity
       }
       else
       {
           Debug.Log("Jump button pressed but player is not grounded");
       }
    2. Physics: Make sure that your Rigidbody2D is set up correctly. The gravity scale should not be zero, and the Rigidbody2D should not be kinematic. Check these settings in the Unity Inspector.

    3. Ground Check: Verify that the groundCheckPoint is properly positioned in your player object. It should be just below the feet of your character. Also, ensure that the whatIsGround LayerMask is correctly assigned to the ground objects.

    4. Animation Parameters: If the “Jumping” checkbox is automatically checked in the Animator, it could be due to the conditions set in your Animator’s state machine. Make sure that the transitions between states (e.g., Idle, Walking, Jumping) are set up correctly and that they respect the conditions you defined (like the “Jumping” boolean).

    5. Debugging: Use Debug.Log statements to check the values returned by Grounded(). For example, you can log the result of each raycast in the Grounded() method to see if they are hitting the ground:

       Debug.DrawRay(groundCheckPoint.position, Vector2.down * groundCheckY, Color.red);
       Debug.DrawRay(groundCheckPoint.position + new Vector3(groundCheckX, 0, 0), Vector2.down * groundCheckY, Color.red);
       Debug.DrawRay(groundCheckPoint.position + new Vector3(-groundCheckX, 0, 0), Vector2.down * groundCheckY, Color.red);
    6. Animation Interference: Ensure that your jump animation does not override the jump logic. In your Animator, check if there are any transitions that might be blocking the jump action or resetting the vertical velocity.

    7. Unity Input: Finally, check the Input settings in Unity (Edit > Project Settings > Input) to ensure that the “Jump” button is correctly set up (usually mapped to the spacebar).

    By verifying these points, you should be able to identify the issue preventing your character from jumping. If you’re still having trouble, consider sharing the specific parts of your Animator setup or any additional code that might influence the jumping behavior.

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

Go to Login Page →


Advertisement below: