1
\$\begingroup\$

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): PPS: It's not super-slow , like being in space. (I've had that problem in LibGDX where my objects scales were all wrong. But this time I have just the standard sized Capsule (1x2 units) and the platform is about 4 units above the ground. He jumps ok, but I'd like to make him jump really-fast like mario can

 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);
         }
     }
 }
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Why not set the velocity of the object instead of applying forces? \$\endgroup\$
    – Bálint
    Dec 13, 2017 at 14:33
  • \$\begingroup\$ Ah, well thanks for the quick answer firstly! However, I used to do that and ran into physics and gravity problems in older projects, and I was told that changing the velocity etc manually will break the physics simulation \$\endgroup\$ Dec 13, 2017 at 14:35
  • 2
    \$\begingroup\$ There't nothing fundamentally wrong above, though you should beware of mixing Transform and Rigidbody movement as a general rule - this rips control away from the physics engine and can break interpolation / collision response / etc. Your use of an impulse for jumping is just fine, though setting velocity would work too. More on that here. Have you tried just cranking up your jumpForce and gravity parameters to make the vertical movement snappier? \$\endgroup\$
    – DMGregory
    Dec 13, 2017 at 18:04

2 Answers 2

1
\$\begingroup\$

I doubt you still need help on this now, but the problem is that you're changing the value of JumpForce in the script. This value gets overridden by the value attached to the object you are applying force to. Try changing the value in the inspector instead.

\$\endgroup\$
0
\$\begingroup\$

The answer turned out to be simple again. And thanks to DMGregorys comments above I found the answer quickly.

Just go to ' Edit> Project Settings> Physics' and play with the gravity and the jumpForce you create and you can achieve different jumping and falling speeds without changing velocity directly yourself in code, and hence disrupting the physics calculations (such as gravity, velocity, collisions etc)

\$\endgroup\$
3
  • \$\begingroup\$ I will recommend against this solution. Changing gravity affects the whole game. This makes the entire game tightly-bound to your physics settings. What would happen if you wanted to change the jumping/falling speed overall in the future? Or what about if wanted to make jumping/falling speed different (buff/nerf) in a specific situation of the game? Everything else is also affected, so you'd have to re-balance the whole game because of it. --- You'd have to script every other object, instead of the player, to normalize things. Much worse. --- This is IMHO a bad solution & practice. \$\endgroup\$ Dec 14, 2017 at 1:34
  • \$\begingroup\$ Let us continue this discussion in chat. \$\endgroup\$ Dec 14, 2017 at 2:24
  • 1
    \$\begingroup\$ Awesome, its 2.30am here now but tomorrow when I at the computer again this will be the first thing I look at. Cheers \$\endgroup\$ Dec 14, 2017 at 2:25

You must log in to answer this question.

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