Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do I make 'Fixed' Jumping (Making the jumping height higher if the player is moving faster)?

Discussion in 'Scripting' started by unity_JKfie4JBOVqowQ, Mar 21, 2021.

  1. unity_JKfie4JBOVqowQ

    unity_JKfie4JBOVqowQ

    Joined:
    Mar 21, 2021
    Posts:
    1
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6. public class Player : MonoBehaviour
    7. {
    8.  
    9.     [SerializeField] private Transform groundCheckTransform = null;
    10.     [SerializeField] private LayerMask playerMask;
    11.     private bool jumpKeyWasPressed;
    12.     private bool SprintWasPressed;
    13.     private float horizontalInput;
    14.     private Rigidbody rigidbodyComponent;
    15.    
    16.  
    17.  
    18.  
    19.     // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.         rigidbodyComponent = GetComponent<Rigidbody>();
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.         if (Input.GetKeyDown(KeyCode.Space))
    29.  
    30.         {
    31.             jumpKeyWasPressed = true;
    32.            
    33.         }
    34.  
    35.         horizontalInput = Input.GetAxis("Horizontal");
    36.  
    37.      
    38.         {
    39.             if (Input.GetKey(KeyCode.LeftShift))
    40.             {
    41.                 SprintWasPressed = true;
    42.             }
    43.  
    44.         }
    45.  
    46.        
    47.     }
    48.  
    49.     // FixedUpdate is called once every phyiscs update
    50.     void FixedUpdate()
    51.     {
    52.         rigidbodyComponent.velocity = new Vector3(horizontalInput * 1, rigidbodyComponent.velocity.y, 0);
    53.  
    54.         if (Physics.OverlapSphere(groundCheckTransform.position, 0.1f, playerMask).Length == 0 )
    55.         {
    56.             return;
    57.         }
    58.  
    59.         if (jumpKeyWasPressed)
    60.  
    61.         {
    62.             rigidbodyComponent.AddForce(Vector3.up * 7, ForceMode.VelocityChange);
    63.             jumpKeyWasPressed = false;
    64.         }
    65.  
    66.         if (SprintWasPressed)
    67.         {
    68.             rigidbodyComponent.velocity = new Vector3(horizontalInput * 3, rigidbodyComponent.velocity.y, 0);
    69.         }
    70.        
    71.        
    72.  
    73.     }
    74.  
    75.  
    76. }
    77.  
    78.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    You could use the player's velocity as the basis for deciding how hard to jump upwards.

    For right now you seem to have a magic number of "3" in for lateral speed, and another magic number of "7" for the force upwards when you jump.

    Instead, assign proper variables and develop a function that converts the current speed into the jump velocity upwards.