1
\$\begingroup\$

Im working on a game 2d game that has some platforming. I followed this video as guidance and produced the following code:

public class PlayerControler : MonoBehaviour


    public float maxWalkingSpeed = 10.0f;
    public float jumpForce = 5.0f;
    public float jumpInputSensitivityThreshold = 0.3f;
    public float verticalAirControl = 0.5f;
    public float horizontalAirControl = 2.0f;
    public Transform groundCheck;
    public float groundCheckRadius;
    public LayerMask whatIsGround;
    private bool facingRight = false;
    private bool isControlable = true;
    private bool canMove = true;
    private bool isGrounded = false;
    private Animator playerAnimator;

    // Use this for initialization
    void Start ()
    {
            playerAnimator = GetComponent<Animator> ();
    }

    // Update is called once per physics calculation
    void FixedUpdate ()
    {
            CheckIfGrounded ();
            CheckInput ();
            CheckHorizontalMovement ();
            CheckVerticalMovement ();
    }

    private void CheckInput ()
    {

            bool isBlockHeld = Input.GetButtonDown ("Block");
            bool isBlockReleased = Input.GetButtonUp("Block");
            bool isPerformingAttack1 = Input.GetButtonDown ("Attack1");
            bool isPerformingAttack2 = Input.GetButtonDown ("Attack2");
            float horizontalInput = Input.GetAxis ("Horizontal");
            float verticalInput = Input.GetAxis ("Vertical");

            if (isControlable) {
                    CheckFacing (horizontalInput);

                    if (isBlockHeld) {
                            if (verticalInput < 0) {
                                    playerAnimator.SetTrigger ("LowBlockPushed");
                            } else {
                                    playerAnimator.SetTrigger ("HighBlockPushed");
                            }
                    }

                    if(isBlockReleased)
                    {
                        playerAnimator.SetTrigger ("BlockReleased");
                    }

                    if(isPerformingAttack1)
                    {
                        playerAnimator.SetTrigger ("Attack1");
                    }

                if(isPerformingAttack2)
        {
            playerAnimator.SetTrigger ("Attack2");
        }

                    if (isGrounded && canMove && horizontalInput != 0) {

                            Walk (horizontalInput);
                    }


                    if (isGrounded && canMove && verticalInput > jumpInputSensitivityThreshold) {
                            Jump (verticalInput);       
                    }

                    if (!isGrounded && canMove) {
                            AirControl (horizontalInput, verticalInput);
                    }
            }


    }

    private void CheckFacing (float horizontalInput)
    {
            if (horizontalInput > 0 && !facingRight) {
                    Flip ();
            } else
        if (horizontalInput < 0 && facingRight) {
                    Flip ();
            }
    }

    private void CheckIfGrounded ()
    {
            isGrounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);
            playerAnimator.SetBool ("isGrounded", isGrounded);
    }

    private void CheckHorizontalMovement ()
    {
            playerAnimator.SetFloat ("HorizontalVelocity", Mathf.Abs (rigidbody2D.velocity.x));
    }

    private void CheckVerticalMovement ()
    {
            playerAnimator.SetFloat ("VerticalVelocity", rigidbody2D.velocity.y);   
    }

    private void AirControl (float horizontalInput, float verticalInput)
    {
            rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x + horizontalInput * horizontalAirControl, rigidbody2D.velocity.y + verticalInput * verticalAirControl);
    }

    private void Jump (float verticalInput)
    {
            rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x, verticalInput * jumpForce);
    }

    private void Walk (float input)
    {
            rigidbody2D.velocity = new Vector2 (input * maxWalkingSpeed, rigidbody2D.velocity.y);
    }

    private void Flip ()
    {
            facingRight = !facingRight;

            Vector3 newScale = transform.localScale;
            newScale.x *= -1;
            transform.localScale = newScale;
    }

    public void setPlayerNotControllable ()
    {
            isControlable = false;
    }

    public void setPlayerControllable ()
    {
            isControlable = true;
    }

    public void setPlayerCanMove ()
    {
            canMove = true;
    }

    public void setPlayerCanNotMove ()
    {
            canMove = false;
    }

In the editor I set the jumpForce to 50 and the characters rigidbody properties gravity scale to 10 and mass to 80 for a snappy jump.

The walking and jumping seems okay enough, with one issue. The first jump made by the character is good, of proper height. But if I keep the jump button pressed or hit it immediately after the character falls to the ground after previous jump, it jumps way too high, basically shoots upward like a rocket. If I let it land and wait a bit and then jump again, the jump is properly high. Any ideas what might be causing this? How can I fix this?

If any further information is needed, please leave me a comment.

\$\endgroup\$
1
  • \$\begingroup\$ a LOT of debugging led me to noticing that every push of the jump button is registered twice, only rarely one click = 1 jump. Also, it seems my isGrounded check is faulty, as even in mid-air I get mixed results on whether I am grounded or not. will investigate further \$\endgroup\$
    – K.L.
    Commented Jan 6, 2015 at 14:07

3 Answers 3

1
\$\begingroup\$

Do not put input-updating in FixedUpdate. Probably it's not a full answer, but at least partly your incorrect jumping is because of this:

Note also that the Input flags are not reset until "Update()", so its suggested you make all the Input Calls in the Update Loop.

\$\endgroup\$
2
\$\begingroup\$

Generally with jumping I use Rigidbody2D.AddForce() rather than making changes the velocity. You may find this is a far less painful way of doing jumping and it will still use your Rigidbody (it's also more natural, when you jump you apply force upwards, you don't change your velocity directly).

Try swapping out the line for:

rigidbody2D.AddForce(Vector2.up * 50);

You will have to fiddle around with your multiplier to get it right, as I remember it tends to be quite a big number.

\$\endgroup\$
3
  • \$\begingroup\$ Sorry, this did not help me.. I experimented with add force, and the results were either the character does not leave ground at all, or it steadily, comparatively slowly lifted up, but still very high (say 10-20 units, while the character is ~ 1,5 unit tall and I want it to jump say 3-5 units high, in a snappy fashion) \$\endgroup\$
    – K.L.
    Commented Jan 4, 2015 at 22:20
  • \$\begingroup\$ @K.L. Sorry this didn't solve the problem, the way to sort this is to adjust your physics such as the strength of the gravitational pull etc. However if you already have this set you probably won't want to be changing in unless you change your entire movement controller. \$\endgroup\$ Commented Jan 5, 2015 at 9:26
  • 1
    \$\begingroup\$ What I think is the issue is that your gravity setting is too low. Increase it and see if that helps \$\endgroup\$ Commented Jan 6, 2015 at 10:18
0
\$\begingroup\$

I found what was wrong, maybe it will help future visitors.

This line of code caused problems:

rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x, verticalInput * jumpForce);

While this one did not:

rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x, jumpForce);

The problem was in the input sensitivity for my controls. Sensitivy on an axis is the speed with which the values returned by the Input reach the actual value the player inputs. I had sensitivity set to 3. So when I jumped for the first time, the input didnt reach the intended level of 1. Instead, it was around 0.18 or 0.2. By the time the character fell to the ground, if i hadn't release the button, the input value would be at 1, and the second jump would be higher.

Of course I found it only once I fixed my isGrounded problem, because in combination those two made the player jump sky-high or even jet-pack.

\$\endgroup\$

You must log in to answer this question.

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