Question by
DameyC · Nov 03, 2015 at 05:50 PM ·
c#jumpingplatformer
How do I set the jump distance and height to my player?
I made my player able to jump for a platformer I am making, but he jumps like he is on the moon. I can't figure out how to make him jump a certain height and distance.
Here is my code:
using UnityEngine; using System.Collections;
public class playerScript : MonoBehaviour { public bool grounded = true; public float jumpPower = 190;
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){
GetComponent<Rigidbody2D>().AddForce(transform.up*jumpPower);
grounded = false;
hasJumped = false;
}
}
}
Comment