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

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #19888
    TheRealKnightFromHollowKnight
    Level 1
    Participant
    Helpful?
    Up
    0
    ::
    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);
            }
        }
    }
    #19889
    Terence
    Level 32
    Keymaster
    Helpful?
    Up
    0
    ::

    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.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: