Get 25% off your next order from the Unity Asset Store: YW86GYBU8S.
Forum begins after the advertisement:
[Part 1] Jump is not working
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [Part 1] Jump is not working
- This topic has 3 replies, 3 voices, and was last updated 3 weeks ago by
Terence.
-
AuthorPosts
-
October 5, 2025 at 9:04 pm #18885::
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { [Header("Horizontal Movement Settings")] private Rigidbody2D rb; [SerializeField] private float walkSpeed = 1; private float xAxis; private float jumpForce = 45; // 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); } }October 5, 2025 at 9:09 pm #18886::Hello, samuele marcelli! It looks like you’re working on a player controller script in Unity. I see that you have a basic setup for horizontal movement. If you’re facing any issues or have specific questions about this code, feel free to ask!
Here are a few things you might want to consider or check:
-
Jumping Mechanic: You have a
jumpForcevariable defined, but it looks like you haven’t implemented the jumping logic yet. If you want to add jumping, you could check for a jump input (like the space bar) and apply a force to the Rigidbody2D. -
Ground Check: To prevent the player from jumping while in the air, you’ll need to implement a ground check. This can be done using raycasting or collision detection.
-
Serialized Fields: If you want to adjust the
walkSpeedandjumpForcefrom the Unity Inspector, make sure to mark them as[SerializeField]. -
Code Formatting: It seems like there are some HTML entities in your code (like
<for<). Make sure your code is clean and properly formatted to avoid syntax errors.
Here's a quick example of how you might implement jumping:
private bool isGrounded; void Update() { GetInputs(); Move(); Jump(); } private void Jump() { if (isGrounded && Input.GetButtonDown("Jump")) // "Jump" is usually mapped to the space bar { rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse); } } private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Ground")) // Make sure your ground objects have the "Ground" tag { isGrounded = true; } } private void OnCollisionExit2D(Collision2D collision) { if (collision.gameObject.CompareTag("Ground")) { isGrounded = false; } }Make sure to tag your ground objects appropriately in Unity. If you have any other questions or need further assistance, just let me know! Happy coding!
October 5, 2025 at 10:18 pm #18890::the console says:” the field PlayerController.jumpForce is assigned but its value is never used. What should i do?
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { [Header("Horizontal Movement Settings")] private Rigidbody2D rb; [SerializeField] private float walkSpeed = 1; private float xAxis; private float jumpForce = 45; // 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); } }October 5, 2025 at 10:24 pm #18892::The warning you are seeing indicates that the
jumpForcevariable is declared and assigned a value, but it is never used in your code. If you intend to implement jumping functionality for your player character, you’ll need to use thejumpForcevalue to make the player jump when a specific input is detected (like pressing the space bar).Here’s how you can modify your
PlayerControllerscript to include jumping functionality:- Update the
GetInputsmethod to check for jump input. - Add a method to handle jumping.
- Apply the
jumpForcewhen the player jumps.
Here’s an example of what the updated code might look like:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { [Header("Horizontal Movement Settings")] private Rigidbody2D rb; [SerializeField] private float walkSpeed = 1; private float xAxis; [Header("Jump Settings")] private float jumpForce = 45; private bool isGrounded; // To check if the player is on the ground // Start is called before the first frame update void Start() { rb = GetComponent(); } // Update is called once per frame void Update() { GetInputs(); Move(); Jump(); } 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 and jump input is pressed if (isGrounded && Input.GetButtonDown("Jump")) { rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse); } } private void OnCollisionEnter2D(Collision2D collision) { // Check if the player is touching the ground if (collision.gameObject.CompareTag("Ground")) { isGrounded = true; } } private void OnCollisionExit2D(Collision2D collision) { // Set isGrounded to false when leaving the ground if (collision.gameObject.CompareTag("Ground")) { isGrounded = false; } } }Explanation:
- Jump Method: The
Jumpmethod checks if the player is grounded and if the jump button (usually the space bar) is pressed. If both conditions are true, it applies an upward force to the player usingAddForce. - Ground Check: The
OnCollisionEnter2DandOnCollisionExit2Dmethods are used to set theisGroundedvariable based on whether the player is touching an object tagged as “Ground”. Make sure to tag your ground objects accordingly in Unity.
This should resolve the warning about the
jumpForcevariable, as it will now be actively used in your code.October 6, 2025 at 9:34 pm #18893October 6, 2025 at 9:39 pm #18894::If jumping is still not working, let’s ensure that everything is set up correctly. Here are some troubleshooting steps you can follow:
-
Check Input Settings:
- Make sure that the “Jump” input is correctly set in Unity’s Input Manager. By default, the “Jump” input is usually mapped to the space bar, but you can check this in:
- Edit > Project Settings > Input Manager
- Look for “Jump” and ensure it has the correct settings.
- Make sure that the “Jump” input is correctly set in Unity’s Input Manager. By default, the “Jump” input is usually mapped to the space bar, but you can check this in:
-
Ground Check:
- Ensure that the ground objects in your scene have a collider (e.g.,
BoxCollider2D,CircleCollider2D) attached to them and are tagged as “Ground”. - You can assign the “Ground” tag by selecting the ground object in the hierarchy, then in the Inspector panel, find the Tag dropdown and select “Ground” or add a new tag if it doesn’t exist.
- Ensure that the ground objects in your scene have a collider (e.g.,
-
Rigidbody2D Settings:
- Make sure your player GameObject has a
Rigidbody2Dcomponent attached. - Check the Rigidbody2D settings, particularly:
- Ensure that the “Gravity Scale” is set to a reasonable value (e.g., 1) to allow the player to fall.
- Ensure “Is Kinematic” is not checked.
- Make sure your player GameObject has a
-
Layer Collision Matrix:
- Ensure that your player and ground layers are set up to collide. You can check this in:
- Edit > Project Settings > Physics 2D
- Look for the Layer Collision Matrix and ensure that the layers for your player and ground are set to collide.
- Ensure that your player and ground layers are set up to collide. You can check this in:
-
Debugging:
- Add some debug logs in your
Jumpmethod to see if it gets triggered:private void Jump() { if (isGrounded && Input.GetButtonDown("Jump")) { Debug.Log("Jump triggered"); rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse); } } - If you see the log message in the console when pressing the jump button but the player doesn’t actually jump, the issue might be with how forces are applied or the Rigidbody2D settings.
- Add some debug logs in your
-
Check for Errors:
- Open the Console in Unity (Window > General > Console) and check if there are any errors or warnings that might indicate other issues with your code or setup.
-
Testing:
- Try simplifying the
Jumpmethod temporarily to just set the velocity directly for testing:private void Jump() { if (isGrounded && Input.GetButtonDown("Jump")) { rb.velocity = new Vector2(rb.velocity.x, jumpForce); } }
- Try simplifying the
By following these steps, you should be able to identify what’s preventing the jump functionality from working. If you are still having issues after trying these suggestions, please provide any errors or additional context, and we’ll continue troubleshooting!
October 6, 2025 at 10:37 pm #18895::@samuelemarcelli paste a copy of your player code over here, and I’ll have a look at it for you if the AI’s solutions are not helping.
-
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: