- Home /
 
jump timer
i was Bring if anybody knew how to set a jump limit or jump timer and see shop on unity is my first time using C shop and I'm not sure how, here is my code
using UnityEngine;
public class PlayerMovement : MonoBehaviour { public Rigidbody rb;
 public float ForwardForce = 800f;
 public float SideWaysForce = 300f;
 public float JumpForce = 200f;
 public LayerMask GroundLayers;
 public BoxCollider col;
 void Start()
 {
     rb = GetComponent<Rigidbody>();
    
 }
 // Update is called once per frame
 void Update()
 {
     //change forward force 
     rb.AddForce(0, 0, ForwardForce * Time.deltaTime);
     if ( Input.GetKey("d") )
     {
         rb.AddForce(SideWaysForce, 0, 0 * Time.deltaTime);
     }
     if (Input.GetKey("a"))
     {
         rb.AddForce(-SideWaysForce, 0, 0 * Time.deltaTime);
     }
     if (Input.GetKey("w"))
     {
         rb.AddForce(0, JumpForce, 0 * Time.deltaTime);
     }
 }
 
               }
also if anybody knows how to define the spacebar instead of using the w key that would be great
Answer by icehex · May 15, 2020 at 04:50 AM
Here's a list of all the KeyCode Properties (scroll down) https://docs.unity3d.com/ScriptReference/KeyCode.html. So you can use any of the Properties to get the key you want if it's in there.
To limit the jump, you could check to see if a certain height was reached. If yes, set the rigidbody velocity to 0. Here's a page that shows you all the rigidbody Properties (velocity is one) and Methods. https://docs.unity3d.com/ScriptReference/Rigidbody.html
Last thing here's some (untested) code that should work.
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour { public Rigidbody rb;
 
  public float ForwardForce = 800f;
  public float SideWaysForce = 300f;
  public float JumpForce = 200f;
  public float jumpLimit = 300f;
  public LayerMask GroundLayers;
  public BoxCollider col;
  void Start()
  {
      rb = GetComponent<Rigidbody>();
     
  }
  // Update is called once per frame
  void Update()
  {
      //change forward force 
      rb.AddForce(0, 0, ForwardForce * Time.deltaTime);
      if ( Input.GetKey("d") )
      {
          rb.AddForce(SideWaysForce, 0, 0 * Time.deltaTime);
      }
      if (Input.GetKey("Space"))
      {
          rb.AddForce(-SideWaysForce, 0, 0 * Time.deltaTime);
      }
      if (Input.GetKey("w"))
      {
          rb.AddForce(0, JumpForce, 0 * Time.deltaTime);
      }
     // This is assuming that 'up' is the Y axis. Change to 'z' if it's the Z axis.
     if (rb.position.y > jumpLimit)
     {
          rb.velocity.y = 0;
     }
  }
 }
 
              Your answer
 
             Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Timer Script Not Keeping Accurate Time? 1 Answer
Unity 5 - Time counter Up script (millisecond precision) UI 1 Answer
How to add a delay to a bomb explostion 1 Answer
Multiple Cars not working 1 Answer