0
\$\begingroup\$

I am making a simple jump script by adding a separate script to the character.

I got the character from the Asset Store and I have left it untouched. I have searched the solution and similar problems to this, and I changed the force or JumpHeight to a pretty high value. But it is still nothing. The logs are printed out just fine.

public class JumpScript : MonoBehaviour {
public Rigidbody rb;
public float JumpHeight = 7.0f;

// Update is called once per frame
void Update () {
    if (Input.GetKeyDown (KeyCode.Space)) {
        Debug.Log ("PRESSSED");
        GetComponent<Rigidbody> ().AddForce (Vector3.up * JumpHeight);
        Debug.Log (GetComponent<Rigidbody> ());
    }
}
\$\endgroup\$
8
  • 1
    \$\begingroup\$ Where is that JumpScript attached to and what do you want to use the rb for? \$\endgroup\$
    – Zibelas
    Aug 24, 2020 at 6:43
  • \$\begingroup\$ To the character of course.@Zibelas \$\endgroup\$
    – Zett
    Aug 24, 2020 at 7:03
  • \$\begingroup\$ 1) Your mass of your rigidbody could be too high. 2) You could try to add the force as an impulse .AddForce (Vector3.up * JumpHeight, ForceMode.Impulse); \$\endgroup\$
    – Zibelas
    Aug 24, 2020 at 7:19
  • \$\begingroup\$ Already tried those things but still no luck. \$\endgroup\$
    – Zett
    Aug 24, 2020 at 7:29
  • \$\begingroup\$ Well, the asset you have linked has no rigidbody, they handle all movement with the MovementInput script. If you comment out the Update method from that script, you will notice that the jump script is working. \$\endgroup\$
    – Zibelas
    Aug 24, 2020 at 8:01

1 Answer 1

1
\$\begingroup\$
  1. Be sure your character has a Rigidbody attached, and no Character Controller (if you're intending to move this character with physics)

  2. Ensure the character's Rigidbody is set to dynamic, and its constraints allow movement on the y axis

  3. Disallow rotation on the Rigidbody, to keep your character from tipping over accidentally

  4. Verify that the character's collider is not interpenetrating with any scenery that might trap them in place

  5. Use velocity changes or impulses to set velocity, not forces


Here's a simple jump script that helps fix a couple of these:

// This tells the editor to enforce "only use this script on objects with Rigidbodies".
[RequireComponent(typeof(Rigidbody))]
public class Jump : MonoBehaviour {

    public float jumpHeight 
    
    [SerializeField, HideInInspector]
    float _jumpVelocity;        
    [SerializeField, HideInInspector]
    Rigidbody _body;
    
    // When you tune the jump height in the inspector, automatically
    // calculate the physics values that will get you to that height.
    void OnValidate() {
        // Cache a reference to our body so we don't have to search at runtime.
        _body = GetComponent<Rigidbody>();
    
        // Solve v(t) = v(0) + a * t for v(t) = 0.
        float secondsToJumpApex = Mathf.Sqrt(-2f * jumpHeight / Physics.gravity.y);

        // Substitute into h(t) = h(0) + v(0) * t + (a/2) * t^2 and solve for v(0).
        _jumpVelocity = Physics.gravity.y * secondsToJumpApex; 
    }

    void Update() {
        // Use a named input set up in the Input Manager, not a hard-coded key.
        if (Input.GetButtonDown("Jump")) {
            // Jumps are instantaneous, so use VelocityChange instead of a gradual push.
            _body.AddForce(jumpVelocity * Vector3.up, ForceMode.VelocityChange);
        }
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .