Character Jump Control

Posted by Abdullah Sorathia on Game Development See other posts from Game Development or by Abdullah Sorathia
Published on 2014-06-02T16:43:19Z Indexed on 2014/06/02 21:49 UTC
Read the original article Hit count: 278

Filed under:

I would like to know how can I control the jump movement of a character in Unity3D. Basically I am trying to program the jump in such a way that while a jump is in progress the character is allowed to move either left or right in mid-air when the corresponding keys are pressed. With my script the character will correctly move to the left when, for example, the left key is pressed, but when the right key is pressed afterwards, the character moves to the right before the movement to the left is completed.

Following is the script:

void Update () {

if(touchingPlatform && Input.GetButtonDown("Jump")){
    rigidbody.AddForce(jumpVelocity, ForceMode.VelocityChange);
    touchingPlatform = false;
    isJump=true;
}

//left & right movement
Vector3 moveDir = Vector3.zero;
if(Input.GetKey ("right")||Input.GetKey ("left")){
    moveDir.x = Input.GetAxis("Horizontal"); // get result of AD keys in X

    if(ShipCurrentSpeed==0)
    {
        transform.position += moveDir * 3f * Time.deltaTime;
    }else if(ShipCurrentSpeed<=15)
    {
        transform.position += moveDir * ShipCurrentSpeed * 2f * Time.deltaTime;
    }else
    {
        transform.position += moveDir * 30f * Time.deltaTime;
    }
}

© Game Development or respective owner

Related posts about unityscript