I just started to make my first game with Unity - a simple Jump 'n Run. I used Rigidbody2D.AddForce
for the jump mechanic. Right now I can jump out of the window, if I hold the UpArrow
down. What I am looking for is an option to maximize the jump height. I tried to build a trigger-box and putting the jumping-script into an OnTriggerStay
-function, but this doesn’t work. As I can’t find an answer in the Manual, Scripting API or the internet I would appreciate some help
That’s my current code:
public class Player : MonoBehaviour
{
public float moveSpeed;
public float jumpPower;
void Update ()
{
if (Input.GetKey(KeyCode.RightArrow))
transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
if (Input.GetKey(KeyCode.LeftArrow))
transform.Translate(-Vector2.right * moveSpeed * Time.deltaTime);
Rigidbody2D rb = GetComponent<Rigidbody2D> ();
if (Input.GetKey(KeyCode.UpArrow))
rb.AddForce(Vector2.up * jumpPower);
}
}
What I found in the Scripting API was AddForce.impulse
. This sounds like what I am looking for. But unfortunately I am not able to implement it.