Forum begins after the advertisement:
[Part 1] Jumping at only 1 set height still
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [Part 1] Jumping at only 1 set height still
- This topic has 1 reply, 2 voices, and was last updated 1 day, 18 hours ago by
Terence.
Viewing 2 posts - 1 through 2 (of 2 total)
-
AuthorPosts
-
September 7, 2026 at 7:40 pm #19888::
using System.Collections; using System.Collections.Generic; using System.Numerics; using UnityEngine; public class PlayerController : MonoBehaviour { [Header("Horizontal Movement Settings")] private Rigidbody2D rb; [SerializeField] private float walkSpeed = 1; private float xAxis; [Header("Ground Check Settings")] [SerializeField] private float jumpForce = 8.5f; [SerializeField] private Transform groundCheckPoint; [SerializeField] private float groundCheckY = 0.2f; [SerializeField] private float groundCheckX = 0.5f; [SerializeField] private LayerMask whatIsGround; // Start is called before the first frame update 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.velocity = new UnityEngine.Vector2(walkSpeed * xAxis, rb.velocity.y); } public bool Grounded() { if(Physics2D.Raycast(groundCheckPoint.position, UnityEngine.Vector2.down, groundCheckY, whatIsGround) || Physics2D.Raycast(groundCheckPoint.position + new UnityEngine.Vector3(groundCheckX, 0, 0), UnityEngine.Vector2.down, groundCheckY, whatIsGround) || Physics2D.Raycast(groundCheckPoint.position + new UnityEngine.Vector3(groundCheckX, 0, 0), UnityEngine.Vector2.down, groundCheckY, whatIsGround) || Physics2D.Raycast(groundCheckPoint.position + new UnityEngine.Vector3(-groundCheckX, 0, 0), UnityEngine.Vector2.down, groundCheckY, whatIsGround)) { return true; } else { return false; } } void Jump() { if(Input.GetButtonDown("Jump") && rb.velocity.y > 0) { rb.velocity = new UnityEngine.Vector2(rb.velocity.x, 0); } if (Input.GetButtonDown("Jump") && Grounded()) { rb.velocity = new UnityEngine.Vector3(rb.velocity.x, jumpForce); } } }September 8, 2026 at 12:41 pm #19889::Your problem is here:
void Jump() { if(Input.GetButtonDownUp("Jump") && rb.velocity.y > 0) { rb.velocity = new Vector2(rb.velocity.x, 0); } if (Input.GetButtonDown("Jump") && Grounded()) { rb.velocity = new Vector3(rb.velocity.x, jumpForce); } }For variable jump height, you want to cut the upward velocity when the player releases the Jump button, not when they initially press it.
-
AuthorPosts
Viewing 2 posts - 1 through 2 (of 2 total)
- You must be logged in to reply to this topic.
Advertisement below: