1
\$\begingroup\$

I am pretty new to Unity so how I did it may not be the best way, but when the character jumps it is inconsistent. When I press the up arrow key really quickly and then stop the jump will be further than it is supposed to be and it starts to glide. I want all the jumps to be the same size and the player can't bounce again until it is on the ground. Also if I start playing to quickly within the first second or two the ball jumps really far but if I wait a couple seconds it bounce like normal.

Here is my code:

    using UnityEngine;
    using System.Collections;

    public class MovePlayer : MonoBehaviour {   

    public float distance;      
    public float fspeed;
    public float uspeed;
    public float addToDistance;
    bool jumping = false;

    //PRIVATE
    private Rigidbody rigidBody;

    void Awake()
    {
        rigidBody = GetComponent<Rigidbody>();
        rigidBody.freezeRotation = true;
    }

    void FixedUpdate()
    {
        if (!jumping)
        {
                if (Input.GetButtonDown ("up")) {
                    rigidBody.AddForce (new Vector3 (0, uspeed, fspeed));
                }

                if (Input.GetButtonDown ("down")) {
                    rigidBody.AddForce (new Vector3 (0, uspeed, -fspeed));
                }

                if (Input.GetButtonDown ("right")) {
                    rigidBody.AddForce (new Vector3 (fspeed, uspeed, 0));
                }

                if (Input.GetButtonDown ("left")) {
                    rigidBody.AddForce (new Vector3 (-fspeed, uspeed, 0));
                }
         }

     }

    void OnCollisionEnter(Collision other)
    {
    if (other.gameObject.CompareTag("Ground"))
        jumping = false;
    }

    void OnCollisionExit(Collision other)
    {
    if (other.gameObject.CompareTag("Ground"))
        jumping = true;
    }                
}
\$\endgroup\$
  • \$\begingroup\$ Have you tried adding a timer or something onto the rigidBody.AddForce (new Vector3 (0, uspeed, -fspeed)); ? it could be getting confused and never un registering that you are not pressing a key. Just a quick thought \$\endgroup\$ – Ryan white Aug 17 '16 at 11:58
  • \$\begingroup\$ Nice structure btw, nice to see your making your own character controller :), +1 for that \$\endgroup\$ – Ryan white Aug 17 '16 at 11:58
3
\$\begingroup\$

Use a ForceMode. Impulse is great for a jumping character, looks realistic as uses mass. And it's not continuous. Also requires a lower value for more effect.

if (Input.GetKey(KeyCode.UpArrow))
{
   rigidBody.AddForce(new Vector3(0, uspeed, fspeed), ForceMode.Impulse);
}

Leaving the force mode parameter empty causes Force to be default as ForceMode which is continuous. Which causes sliding more than inertia does.

For ignoring inertial sliding nullify velocity when touching ground.

void OnCollisionEnter(Collision other)
{
    if (other.gameObject.tag == "Ground")
    {
        jumping = false;

        rigidBody.velocity = Vector3.zero;
    }        
}

Checking !jumping before input already rules out jumping on air.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.