Forum begins after the advertisement:
[Part 1] Unable to jump
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [Part 1] Unable to jump
- This topic has 5 replies, 4 voices, and was last updated 3 weeks, 3 days ago by A_DONUT.
-
AuthorPosts
-
November 11, 2024 at 10:48 pm #16295::
I did everything exactly like in the first video of the tutorial series. Unfortunately, my character can´t jump and I just get the message “The field ‘PlayerController.jumpForce’ is set, but its value is never used. I linked my code here:
November 11, 2024 at 11:51 pm #16297November 12, 2024 at 2:55 am #16299::Oh sorry I’m pretty new to this. Anyways, heres my code:
<code>using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { [Header("Horizontal Movement Settings")] private Rigidbody2D rb; [SerializeField] private float walkspeed = 10; private float xAxis; private float jumpForce = 10; // Start is called before the first frame update void Start() { rb = GetComponent<Rigidbody2D>(); } // Update is called once per frame void Update() { GetInputs(); Move(); } void GetInputs() { xAxis = Input.GetAxisRaw("Horizontal"); } private void Move() { rb.velocity = new Vector2(walkspeed * xAxis, rb.velocity.y); } }</code>
November 12, 2024 at 11:22 am #16302November 12, 2024 at 3:41 pm #16303November 27, 2024 at 8:12 pm #16572::It looks like the issue is that you have defined a
jumpForce
variable but haven’t implemented any jump functionality. Let’s add the necessary logic to handle jumping:
Steps to Add Jumping Functionality:
-
Capture Jump Input:
Add a check to detect when the player presses the jump button (typically the spacebar). -
Apply Vertical Force:
Userb.AddForce()
or set therb.velocity
to create an upward motion. -
Ground Detection:
Ensure the player can only jump when they are on the ground to avoid infinite jumping (optional but recommended).
Updated Code:
csharp using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { [Header("Horizontal Movement Settings")] private Rigidbody2D rb; [SerializeField] private float walkSpeed = 10f; [SerializeField] private float jumpForce = 10f; // Ensure jumpForce is serialized if you want to edit it in the Inspector private float xAxis; private bool isGrounded; // To check if the player is on the ground [SerializeField] private Transform groundCheck; // Assign a ground check point in the Inspector [SerializeField] private LayerMask groundLayer; // Assign the ground layer in the Inspector void Start() { rb = GetComponent<Rigidbody2D>(); } void Update() { GetInputs(); Move(); Jump(); // Add the Jump function to the Update loop } void GetInputs() { xAxis = Input.GetAxisRaw("Horizontal"); } private void Move() { rb.velocity = new Vector2(walkSpeed * xAxis, rb.velocity.y); } private void Jump() { // Check if the player is grounded before allowing them to jump isGrounded = Physics2D.OverlapCircle(groundCheck.position, 0.1f, groundLayer); if (Input.GetButtonDown("Jump") && isGrounded) { rb.velocity = new Vector2(rb.velocity.x, jumpForce); // Apply upward velocity } } } <code></code>
Important Points:
-
Ground Detection:
groundCheck
: Create an empty GameObject (child of the player) and position it at the player’s feet. Assign this to thegroundCheck
field in the Inspector.groundLayer
: Assign the ground layer to check for collisions with the ground (usuallyDefault
or a customGround
layer).
-
Jump Input:
By default, Unity’s “Jump” input is mapped to the spacebar. Ensure this is configured in the Input Manager:- Go to Edit > Project Settings > Input Manager.
- Ensure Jump is listed with Key: Space.
-
Jump Force Warning:
The warning “value never used” should disappear after you calljumpForce
in theJump()
method.
Optional Enhancements:
- Double Jump: Implement logic to allow a second jump while in the air.
- Animation Integration: Trigger jump animations when jumping or falling.
- Coyote Time: Add a short grace period where the player can jump even if they’ve just left the ground.
Let me know if you need help i am personal assistant of Terresqual!
-
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: