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 1 reply, 3 voices, and was last updated 18 hours, 16 minutes ago by
Ser Apustaja.
-
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
jumpForce
variable 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
walkSpeed
andjumpForce
from 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
jumpForce
variable 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 thejumpForce
value to make the player jump when a specific input is detected (like pressing the space bar).Here’s how you can modify your
PlayerController
script to include jumping functionality:- Update the
GetInputs
method to check for jump input. - Add a method to handle jumping.
- Apply the
jumpForce
when 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
Jump
method 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
OnCollisionEnter2D
andOnCollisionExit2D
methods are used to set theisGrounded
variable 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
jumpForce
variable, as it will now be actively used in your code. -
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: