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 10 replies, 6 voices, and was last updated 1 month ago by
Terence.
-
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!
April 4, 2025 at 8:23 am #17817::help i have in issue in the word position it says “float doesnt have a definition for position
using UnityEngine; public class Playercontroller : MonoBehaviour { [Header("Horizontal Movement Settings")] private Rigidbody2D rb; [SerializeField] private float walkSpeed = 1; [SerializeField] private float jumpForce = 35; private float xAxis; private bool isGrounded; [Header("Ground Check Settings")] [SerializeField] private Transform groundCheckPoint; [SerializeField] private float groundCheck = 0.2f; [SerializeField] private LayerMask groundLayer; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { rb = GetComponent<Rigidbody2D>(); } // Update is called once per frame void Update() { GetInputs(); Move(); Jump(); } void GetInputs() { xAxis = Input.GetAxisRaw("Horizontal"); } private void Move() { rb.linearVelocity = new Vector2(walkSpeed * xAxis, rb.linearVelocity.y); } private void Jump() { isGrounded = Physics2D.OverlapCircle(groundCheck.position, 0.1f, groundLayer); if (Input.GetButtonDown("Jump") && isGrounded) { rb.velocity = new Vector2(rb.velocity.x, jumpForce); } } }
April 4, 2025 at 8:26 am #17818::The error you’re encountering, “float doesn’t have a definition for position,” is likely because you’re trying to access the
position
property ofgroundCheck
, which you have declared as aTransform
. However, in yourJump()
method, you are mistakenly usinggroundCheck
instead ofgroundCheckPoint
.Here’s how you can fix this issue:
- Change the
groundCheck
variable in theJump()
method togroundCheckPoint
.
Here’s the corrected version of the
Jump()
method:private void Jump() { // Use groundCheckPoint instead of groundCheck isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, 0.1f, groundLayer); if (Input.GetButtonDown("Jump") && isGrounded) { rb.velocity = new Vector2(rb.velocity.x, jumpForce); } }
Additionally, make sure you replace the
&&
with&&
for the logical AND operator in yourif
statement. The corrected line should look like this:if (Input.GetButtonDown("Jump") && isGrounded)
With these changes, your code should work without the error you were experiencing.
April 4, 2025 at 9:28 am #17819::i have an issue with the word groundCheckY, and groundCheckX on the code down here
using UnityEngine; public class Playercontroller : MonoBehaviour { [Header("Horizontal Movement Settings")] private Rigidbody2D rb; [SerializeField] private float walkSpeed = 1; [SerializeField] private float jumpForce = 35; private float xAxis; private bool isGrounded; [Header("Ground Check Settings")] [SerializeField] private Transform groundCheckPoint; [SerializeField] private float groundCheckY = 0.2f; [SerializeField] private float groundCheckX = 0.5f; [SerializeField] private LayerMask groundLayer; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { rb = GetComponent<Rigidbody2D>(); } // Update is called once per frame void Update() { GetInputs(); Move(); Jump(); } void GetInputs() { xAxis = Input.GetAxisRaw("Horizontal"); } private void Move() { rb.linearVelocity = new Vector2(walkSpeed * xAxis, rb.linearVelocity.y); } private void Jump() { isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, 0.1f, groundLayer); if (Input.GetButtonDown("Jump") && isGrounded) { rb.linearVelocity = new Vector2(rb.linearVelocity.x, jumpForce); } } }
April 4, 2025 at 9:31 am #17820::It looks like you are trying to use
groundCheckY
andgroundCheckX
in your code, but they are not being used anywhere yet. If you want to incorporate these variables into your ground checking logic, you might want to use them to define the size of the area in which you check for ground.Here’s an updated version of your
Jump()
method that usesgroundCheckY
andgroundCheckX
to create an area for checking if the player is grounded:private void Jump() { // Create a rectangle for ground checking using groundCheckY and groundCheckX isGrounded = Physics2D.OverlapBox(groundCheckPoint.position, new Vector2(groundCheckX, groundCheckY), 0f, groundLayer); if (Input.GetButtonDown("Jump") && isGrounded) { rb.velocity = new Vector2(rb.velocity.x, jumpForce); } }
In this code:
OverlapBox
is used instead ofOverlapCircle
. It checks if there are any colliders within a rectangular area defined by the width (groundCheckX) and height (groundCheckY) centered atgroundCheckPoint.position
.- The
0f
argument is the angle of rotation, which is not needed in this case, so we set it to zero.
Make sure you also change the logical AND operator from
&&
to&&
to avoid errors.This way, you can utilize the
groundCheckY
andgroundCheckX
fields for more precise ground checking. Ensure that yourgroundCheckPoint
is positioned correctly in the game so that it checks the right area for ground detection.April 5, 2025 at 2:12 pm #17836::Your code should be:
private void Jump() { isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position,
0.1fgroundCheck, groundLayer); if (Input.GetButtonDown("Jump") && isGrounded) { rb.velocity = new Vector2(rb.velocity.x, jumpForce); } }Do follow the naming of the variables in the videos / articles more closely. Otherwise, you will get a lot more issues later on.
-
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: