c# – How to jump in unity

First you need to save the rigidbody instance to increase performance as well as access it:

private Rigidbody rb; // define rigidbody holder

private void Start()
    rb = GetComponent<Rigidbody>(); save instance on rb

This code is a simple jump with adjustable power. In most cases with `mass = 1`, a `jumpPower` of 300 is sufficient.

public float jumpPower = 300f;
private void Jump(float jumpPower) => rb.AddForce(Vector3.up * jumpPower);

Finally, by pressing the Space key, the jump method is executed.

private void Update()
    // some codes....

    if (Input.GetKeyDown(KeyCode.Space)) Jump(jumpPower);

Source link

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top