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
- This topic has 0 replies, 2 voices, and was last updated 19 hours, 42 minutes ago by Ser Apustaja.
-
AuthorPosts
-
January 9, 2025 at 5:21 pm #17083::
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
January 9, 2025 at 7:12 pm #17085::It looks like you’re having issues with the jumping functionality in your Unity character controller. Let’s break down potential problems and solutions based on your code.
1. Jumping Logic
In your
Jump()
method, you have the following lines:if (Input.GetButtonUp("Jump") && rb.linearVelocity.y > 0) { rb.linearVelocity = new Vector2(rb.linearVelocity.x, 0); }
This line resets the vertical velocity to
0
when the jump button is released, but only if the player is already moving upwards. This can lead to unexpected behavior, as it essentially cancels the jump prematurely. You might want to remove this section entirely, or adjust it to only affect the jump if the player is falling or has reached the top of the jump.2. Jumping Condition
The condition for jumping is as follows:
if (Input.GetButtonDown("Jump") && Grounded())
Make sure that your
Grounded()
method is correctly identifying when the player is on the ground. You can debug theGrounded()
method by logging the result to ensure it’s returningtrue
when the player is actually touching the ground.3. Animator Parameters
If your jump animation is not being triggered correctly, check that your Animator parameters
"Jumping"
and"Walking"
are set up correctly. Make sure that the transitions in your Animator controller are properly configured to respond when these parameters change.4. Preventing Jump Checkbox in Animator
For your second question regarding the “Jumping” checkbox automatically being checked in the Animator’s parameters menu, this could happen if your jump animation is set to be a looping animation or if it’s set to automatically transition based on the conditions you’ve set. Make sure that your jump animation is not set to loop unless that is your intention.
Updated Jump Logic Example
Here’s an example of how you might restructure your
Jump()
method:private void Jump() { if (Input.GetButtonDown("Jump") && Grounded()) { Debug.Log("Jump button pressed and player is grounded"); rb.velocity = new Vector2(rb.velocity.x, jumpForce); } }
Full Example of PlayerController
Here’s a combined revised version of your
PlayerController
class: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 Animator anim; void Start() { rb = GetComponent(); anim = GetComponent(); } void Update() { GetInputs(); Move(); Jump(); UpdateAnimations(); } void GetInputs() { xAxis = Input.GetAxisRaw("Horizontal"); } private void Move() { rb.velocity = new Vector2(walkSpeed * xAxis, rb.velocity.y); } private void Jump() { if (Input.GetButtonDown("Jump") && Grounded()) { Debug.Log("Jump button pressed and player is grounded"); rb.velocity = new Vector2(rb.velocity.x, jumpForce); } } private void UpdateAnimations() { anim.SetBool("Walking", rb.velocity.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; } }
Conclusion
Make sure to test the adjustments made in the jumping logic and check the Animator settings. If the problem persists, consider debugging the ground check logic and confirming that the Rigidbody2D component and collider settings are configured correctly.
If you have further questions or need more assistance, feel free to ask!
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: