Search Unity

Jump Script

Discussion in 'Scripting' started by JulianGN, Apr 26, 2015.

  1. JulianGN

    JulianGN

    Joined:
    Apr 26, 2015
    Posts:
    14
    I am trying to make a very simple 3D plat-former with side to side movement and jumping; however, nothing seems to work when it comes to jumping. This code would work just fine if it weren't for the, roughly, one-in-three chance of launching way to high in the air. I searched ideas for jump scripts and saw that other people have had this same problem but I never saw a solution. Can anyone help?



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class playerMove : MonoBehaviour {
    5.  
    6.     public float jump;
    7.     public float speed;
    8.    
    9.     Rigidbody rb;
    10.    
    11.     // Use this for initialization
    12.     void Start () {
    13.        
    14.         rb = GetComponent<Rigidbody>();
    15.        
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void FixedUpdate () {
    20.        
    21.        
    22.         if (Input.GetKey("right"))
    23.         {
    24.  
    25.             rb.AddForce(speed * Time.deltaTime, 0, 0);
    26.  
    27.         }
    28.  
    29.         if (Input.GetKey("left"))
    30.         {
    31.  
    32.             rb.AddForce(-speed * Time.deltaTime, 0, 0);
    33.  
    34.         }
    35.  
    36.         if (Input.GetKeyDown("up"))
    37.         {
    38.             rb.AddForce(0, jump, 0);
    39.         }
    40.     }
    41. }
    42.  
    Thanks for any and all help.
     
  2. JulianGN

    JulianGN

    Joined:
    Apr 26, 2015
    Posts:
    14
    Can anyone help?
     
  3. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Have you tried using a stored vector???

    This works for me.....
    Code (csharp):
    1.  
    2. rigidbody.AddForce(Vector3.up * step, ForceMode.Impulse);
     
  4. JulianGN

    JulianGN

    Joined:
    Apr 26, 2015
    Posts:
    14
    Unfortunately, that isn't working.
     
  5. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Why not? Does step have a value? Are you assigning the correct rigidbody? Is your rigidbody kinematic? It's a very simple application of code that does work. I use it frequently.
     
  6. JulianGN

    JulianGN

    Joined:
    Apr 26, 2015
    Posts:
    14
    It works just fine, but it doesn't solve the problem of it jump way to high every few jumps.
     
  7. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    You have no grounded check. You're gonna need a raycast to check if your character is grounded before jumping again.
     
  8. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Or set the velocity to zero.
     
  9. JulianGN

    JulianGN

    Joined:
    Apr 26, 2015
    Posts:
    14
    I intend to do that, but I am letting the ball fully land and sit on the ground before jumping again.
     
  10. JulianGN

    JulianGN

    Joined:
    Apr 26, 2015
    Posts:
    14
  11. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    hamsterbytedev and Fajlworks like this.
  12. JulianGN

    JulianGN

    Joined:
    Apr 26, 2015
    Posts:
    14
    Thank you, Korno!!

    It now works without flying into space every few times.
     
  13. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Youre welcome, by the way you should keep your AddForce in FixedUpdate as thats the place for all physics stuff. Best to set a flag in Update to tell FixedUpdate to jump, then reset that flag in FixedUpdate after it has done the jump.
     
  14. JulianGN

    JulianGN

    Joined:
    Apr 26, 2015
    Posts:
    14
    I don't know how to tell FixedUpdate to do something from inside Update; could you give an example?
     
  15. imtehQ

    imtehQ

    Joined:
    Feb 18, 2013
    Posts:
    232
    you cant do it inside update,
    Fixedupdate will run at its own speed.

    1. void Update()
    2. {

    3. }
    4. void FixedUpdate()
    5. {

    6. }
     
  16. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Code (CSharp):
    1. void Update() {
    2.      if(Input.GetButtonDown("Jump"){
    3.           hasJumped = true;
    4.      }
    5. }
    6.  
    7. void FixedUpdate() {
    8.      if(hasJumped){
    9.           //Do something while jumping
    10.      }
    11.      If(isGrounded){
    12.           hasJumped = false;
    13.      }
    14. }
     
  17. JulianGN

    JulianGN

    Joined:
    Apr 26, 2015
    Posts:
    14
    Thank you