Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to make the player jump when space bar is pressed?

Discussion in 'Editor & General Support' started by Nathanaji01, Jun 21, 2019.

  1. Nathanaji01

    Nathanaji01

    Joined:
    Oct 8, 2017
    Posts:
    24
    I know this is a very simple question for some people, but there are a few complications.

    1. The player is moving based on camera direction (where the camera is pointing to)

    2. The player floats slightly above the ground (I don't know if this is a complication but it is something I want to know how to fix)

    3. The camera follows the player


    This the code for the player movement:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Player : MonoBehaviour {
    5.     private Rigidbody rb;
    6.     public int baseSpeed;
    7.     public int sprint;
    8.     private int currentSpeed;
    9.  
    10.     void Start ()
    11.     {
    12.         rb = GetComponent<Rigidbody>();
    13.     }
    14.     void Update ()
    15.     {
    16.         //this makes the player go where the camera is pointing
    17.         transform.position = transform.position + Camera.main.transform.forward*currentSpeed*Time.deltaTime;
    18.  
    19.         //this makes the player sprint when the shift key is pressed
    20.         if (Input.GetKey(KeyCode.LeftShift))
    21.         {
    22.             currentSpeed = baseSpeed + sprint;
    23.         }
    24.         else
    25.         {
    26.             currentSpeed = baseSpeed;
    27.         }
    28.     }
    29. }
    This is the code for the camera:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Orbit : MonoBehaviour {
    4.    
    5.      public float turnSpeed = 4.0f;
    6.      public Transform player;
    7.    
    8.      public float height = 1f;
    9.      public float distance = 2f;
    10.    
    11.      private Vector3 offsetX;
    12.    
    13.      void Start () {
    14.        
    15.          offsetX = new Vector3(0, height, distance);
    16.      }
    17.    
    18.      void LateUpdate()
    19.      {
    20.          offsetX = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offsetX;
    21.          transform.position = player.position + offsetX;
    22.          transform.LookAt(player.position);
    23.      }
    24. }
    Please also help me fix errors in the code as well. Your help is greatly apprecieted.
     
  2. ManuelSegura

    ManuelSegura

    Joined:
    Dec 12, 2015
    Posts:
    19
    I guess you can start with something like this:

    Code (CSharp):
    1. // New Variable
    2. public float jumpForce;
    3.  
    4. // Inside Update
    5. if (Input.GetKeyDown(KeyCode.Space))
    6. {
    7. rb.velocity = new Vector3 (rb.velocity.x,jumpForce,rb.velocity.z);
    8. }
     
  3. Nathanaji01

    Nathanaji01

    Joined:
    Oct 8, 2017
    Posts:
    24
    Thank you very much, this worked. But when I press space while my player is in the air, the player jumps again in the air. How do I fix this? I saw a forum on how someone made a function called "isGrounded". But when I tried this it didn't seem to work. Also when I am making the player sprint (by pressing shift), the "jump" does not work.
     
  4. ManuelSegura

    ManuelSegura

    Joined:
    Dec 12, 2015
    Posts:
    19
    Try to use a raycast. You can put it in a function so you don't have to call it all the time, just when needed, but I'm lazy today:

    Code (CSharp):
    1.  // New Variables
    2. RaycastHit hit;
    3.         float distanceToGround = 0.5f;
    4.         bool isGrounded = false;
    5.  
    6. // Put this inside Update
    7.         if (Physics.Raycast(transform.position, Vector3.down, out hit, distanceToGround, 1 >> 8))
    8.         {
    9.             isGrounded = true;
    10.         }
    11.         else
    12.         {
    13.             isGrounded = false;
    14.         }
    I have no idea why it doesn't jump when you sprint, if it does it normally it should do it sprinting too. Either way, there are literally hundreds of tutorials in Youtube on how to make simple platformers, which are probably gonna help you way more than me.
     
    yong2khoo likes this.
  5. Nathanaji01

    Nathanaji01

    Joined:
    Oct 8, 2017
    Posts:
    24
    Thank you very much. This worked perfectly. Also when I am making the player sprint (by pressing shift), the "jump" does work like normal. Thank you very much.
     
  6. GauravvSh

    GauravvSh

    Joined:
    Apr 2, 2021
    Posts:
    1
    Player shouldn't take next jump , untill the completion of last hump , can you add this constraint.
     
  7. DaBabyDrxgon

    DaBabyDrxgon

    Joined:
    Nov 16, 2021
    Posts:
    1
    can someone tell me how to bind my jump after make the script
     
  8. bro_went_rage_mode

    bro_went_rage_mode

    Joined:
    Aug 13, 2023
    Posts:
    1

    thanks I'm a newbie
     
  9. RafaelCorreia2002

    RafaelCorreia2002

    Joined:
    Oct 2, 2023
    Posts:
    1

    omg
    thank you so much
    it worked.