0
\$\begingroup\$

I can now control my character in air, I used Unity's CharacterController.Move docs, but now I need to make my character double jump.

Here's the code.

public class PlayerMovement : MonoBehaviour
{

    // Moving fields
    float speed = 6;
    float jumpSpeed = 8;
    float gravity = 20;
    Vector3 moveDirection = Vector3.zero;
    CharacterController controller;
    bool isJumping;

    void Start()
    {
        controller = GetComponent<CharacterController>();
    }

    void Update()
    {
        if (controller.isGrounded)
        {
            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection *= speed;
            if (Input.GetButton("Jump")) moveDirection.y = jumpSpeed;
        }
        else
        {
            moveDirection.x = Input.GetAxis("Horizontal") * speed;
            moveDirection.z = Input.GetAxis("Vertical") * speed;
        }
        moveDirection.y -= gravity * Time.deltaTime;
        controller.Move(moveDirection * Time.deltaTime);
    }
}
\$\endgroup\$
3
\$\begingroup\$

I tried your script and it seems like you're not checking if the "jump" button is pressed while in the air. The easiest way to fix this would be to add the line

if (Input.GetButton("Jump")) moveDirection.y = jumpSpeed;    

to the else statement in your update function.

I would also recomend you to use Input.GetButtonDown insted of Input.GetButton.

Input.GetButton will trigger as long as you hold the "Jump" button down, making the player fly to infinity if you hold the button long enough.

Input.GetButtonDown on the other hand will only trigger once each time you press the "Jump" button, making the player jump accordingly to your jumpSpeed variable.

And if you only need double jumping and not "infinity jumping" you need some sort of counter to keep track on how many times you've double jumped.

A good coding custom is to avoid repetitions in the code. The movement in your if and else statements seems to be doing the same thing. So it's only needed once, outside of the if/else statement.

Here is the code i wrote to add the double jump functionality you asked for.

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {
    // Moving fields
    [SerializeField] // This will make the variable below appear in the inspector
    float speed = 6;
    [SerializeField]
    float jumpSpeed = 8;
    [SerializeField]
    float gravity = 20;
    Vector3 moveDirection = Vector3.zero;
    CharacterController controller;
    //bool isJumping; // "controller.isGrounded" can be used instead
    [SerializeField]
    int nrOfAlowedDJumps = 1; // New vairable
    int dJumpCounter = 0;     // New variable

    void Start()
    {
        controller = GetComponent<CharacterController>();
    }
    void Update()
    {
        moveDirection.x = Input.GetAxis("Horizontal") * speed;
        moveDirection.z = Input.GetAxis("Vertical") * speed;

        if (Input.GetButtonDown ("Jump")) {
            if (controller.isGrounded) {
                moveDirection.y = jumpSpeed;
                dJumpCounter = 0;
            }
            if (!controller.isGrounded && dJumpCounter < nrOfAlowedDJumps) {
                moveDirection.y = jumpSpeed;
                dJumpCounter++;
            }
        }
        moveDirection.y -= gravity * Time.deltaTime;
        controller.Move(moveDirection * Time.deltaTime);
    }
}

I hope this has answerd your question.

\$\endgroup\$
  • 1
    \$\begingroup\$ I know this is very old but your answer, Sandsten, has just saved my day. Thank you! \$\endgroup\$ – CPT NINGLE Nov 17 at 20:39

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.