- Home /
Changing the speed and accelleration of a jump using the built-in Physics Rigidbody engine in Unity?
Hi all, I have started making a basic platformer game just for fun. Its going to be the style of the original Super Mario Bros game, but with 3d assets.
My problem is that I'm using a rigidbody and using code I have always used to make my player object jump. It seems really slow. I am using a capsule object, the standard size (which is 2 units height). I have played about with the mass, ive tried 1 and 40. And other than having to increase the 'jumpForce' value accordingly, it seems to have not changed the jump speed at all.
So I'm hoping there is a way to tweak the flight of the jump whilst still using the Rigidbody component, and if so how please?
If not, I think I'm able to write my own code to make the gravity etc, but this seems stupid since Unity is a well established game framework I expect other people have also needed to change it and I'm sure there is a way I just don't know about.
Hope someone can help me.
Many thanks!
PS (Here is the basic code I have to make the character jump):
public class PlayerController : MonoBehaviour {
Rigidbody rb;
Vector3 faceRight = new Vector3(0, 90, 0);
Vector3 faceLeft = new Vector3(0, -90, 0);
float halfHeight;
float speed = Constants.speed;
float jumpForce = Constants.jumpForce;
private bool IsGrounded()
{
return Physics.Raycast(transform.position, Vector3.down, halfHeight + 0.1f);
}
void Start () {
halfHeight = GetComponent<Collider>().bounds.extents.y;
rb = GetComponent<Rigidbody>();
rb.transform.Rotate(0, 90, 0); // make the capsule face right at start.
}
void Update () {
HandleKeys();
}
void HandleKeys()
{
if (Input.GetKey(KeyCode.A))
{
if (rb.transform.eulerAngles != faceLeft)
rb.transform.eulerAngles = faceLeft;
rb.transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
if (rb.transform.eulerAngles != faceRight)
rb.transform.eulerAngles = faceRight;
rb.transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
if (Input.GetKeyDown(KeyCode.Space) && IsGrounded())
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
}