Search Unity

Trying to add a jump limit / max height when pressing space once/ trying to stop access jumping

Discussion in 'Scripting' started by Pryamus, Dec 9, 2018.

  1. Pryamus

    Pryamus

    Joined:
    May 24, 2018
    Posts:
    10
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class playermovement : MonoBehaviour
    5. {
    6.  
    7.     public Rigidbody rb;
    8.     public float forwardForce = 50f;
    9.     public float variableForce =2f;
    10.     public float jumpLimit =2f;
    11.  
    12.     // Use this for initialization
    13.     private void Start()
    14.     {
    15.         Debug.Log("hello world");
    16.         rb = GetComponent<Rigidbody>();
    17.  
    18.     }
    19.  
    20.     private void FixedUpdate()
    21.     {
    22.         if (Input.GetKey(KeyCode.A))
    23.             rb.AddForce(Vector3.left * forwardForce);
    24.         if (Input.GetKey(KeyCode.D))
    25.             rb.AddForce(Vector3.right * forwardForce);
    26.         if (Input.GetKey(KeyCode.W))
    27.             rb.AddForce(Vector3.forward * forwardForce);
    28.         if (Input.GetKey(KeyCode.S))
    29.             rb.AddForce(Vector3.back * forwardForce);
    30.         if (Input.GetKey(KeyCode.Space))
    31.             rb.AddForce(Vector3.up * variableForce, ForceMode.Impulse);
    32.  
    33.         else
    34.             rb.AddForce(-Vector3.up * variableForce, ForceMode.Impulse);
    35.     }
    36.      
    37.        
    38.  
    39.  
    40.        
    41.         }
     
  2. Do you have a question?
     
    SparrowGS likes this.
  3. Pryamus

    Pryamus

    Joined:
    May 24, 2018
    Posts:
    10
    Thank you for asking, Its a complex question but bare with me.

    I got my cube to start using WASD to move around and recently got it to jump with space bar. Now the problem is if i continue to press space bar key it jumps higher and higher. i dont like that and dont want it to do that. i want it to have the ability to only jump one or two spaces higher from its current position but i dont know how to tell it too. :( can you help me out?
     
  4. Use Input.GetKeyDown instead of Input.GetKey for things which you don't want to execute continuously while the key is pressed (like one time events like jump).
     
    SparrowGS likes this.
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    You need a way of detecting if the player is grounded or not, unless you want air jumps.

    Pitching to what @LurkingNinjaDev said, you also need to move the GetKeyDown to Update instead of FixedUpdate,
    Input is detected per visual frame so you might miss a key if it's in FixedUpdate, this is true for GetKeyDown & GetKeyUp(the button variants too) but this does not effect the GetKey because it returns true AS LONG as the key is held.

    something like this should do:
    Code (CSharp):
    1. bool jump = false;
    2.  
    3. void Update(){
    4.  
    5. if(Input.GetKeyDown(KeyCode.Space))
    6. jump = true;
    7. }
    8.  
    9. void FixedUpdate(){
    10.  
    11. if(jump){
    12. Jump();
    13. jump = false;
    14. }
    also, remove the
    Code (CSharp):
    1. else
    2.             rb.AddForce(-Vector3.up * variableForce, ForceMode.Impulse);
    3.  
    it's pushing your player to the ground with the same force as the jump, you have gravity for that.
     
    Lurking-Ninja likes this.
  6. Pryamus

    Pryamus

    Joined:
    May 24, 2018
    Posts:
    10
    keeps coming up as an error, how would i fix that?
     
  7. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    You would make a function called jump or pur your jump code instead if it.

    On a side note, if you cant tell that by your self i suggest you go learn more about C# and programming in general