# How do I set the Jump distance and height of my sprite in C#

I have been able to make my sprite jump for a platformer I am making in unity. I can't seem to be able to make him jump a certain height or distance though, every time he jumps it's like he's jumping on the moon, any suggestions? Here's my code:

using UnityEngine;
using System.Collections;

public class playerScript : MonoBehaviour {
public bool grounded = true;
public float jumpPower = 190;
public float jumpSpeed = 100f;

private bool hasJumped = false;

// Use this for initialization
void Start () {
}

// Update is called once per frame
void Update () {
// Do something

if(!grounded && GetComponent<Rigidbody2D>().velocity.y == 0) {
grounded = true;
}
if (Input.GetKey(KeyCode.Space) && grounded == true) {
hasJumped = true;
}

void FixedUpdate (){
if(hasJumped){
grounded = false;
hasJumped = false;
}
}


Any help would be greatly appreciated :)

• You seem to be applying force in two places, once in the Update and again (jumpspeed) and again in fixed update (jumpPower). Also, depending on what you've set the gravity to, 100f may be too much force. – SanSolo Nov 4 '15 at 8:35

By default AddForce() is applied over time, not in an instant (which is what you want). You can change that though by using the second parameter, such as...

AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse)

Another way that is probably more common, is to just set the velocity instead. This doesn't quite interact with the physics in the same way though, so it's up to you and what you need from the jumping behaviour.

You should try both.

If you want the player to jump a specific height or distance, then velocity is probably going to be easier for you. It's trickier to calculate how a force is going to affect your jump compared to using velocity.

Here is a question on the Unity site that talks about your specific issue.

• Thank you, that helped me with getting my sprite to jump the height I want, but I still got issues with the distance, he moves as fast and as far as he does when he's on the ground. I want him to be able to only go so far once he's airborne. I've been playing around with a few things but I've got nothing, could you help me with that? – Damian Campbell Nov 4 '15 at 9:55