It is impossible to jump in a ball and move simultaneously

I can jump or move left / right at any given time. But it cannot jump and at the same time move left / right at the same time. Did I miss something? My codes are as follows. Thanks for the advice.

public int rotationSpeed = 100;
public float jumpHeight = 8;

private bool isFalling = false;

void Update () {
    // handle ball rotation 
    float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
    rotation *= Time.deltaTime;
    rigidbody.AddRelativeTorque(Vector3.back * rotation);  

    //check input
    if (Input.GetKeyDown(KeyCode.W)) 
    {
        rigidbody.velocity = new Vector3(0, jumpHeight, 0); 
    }
}
+4
source share
1 answer

As stated in the docs: here

In most cases, you do not need to change the speed directly; you should use AddForce instead of AddForceAtPosition

EDIT:

, : Velocity - , , , , , AddForce ,

, , , , AddForce, ,

2:

void Update () {
    // handle ball rotation 
    float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
    rotation *= Time.deltaTime;
    rigidbody.AddRelativeTorque(Vector3.back * rotation);  

    //check input
    if (Input.GetKeyDown(KeyCode.W)) 
    {
        rigidbody.AddForce(new Vector3(0, jumpHeight, 0) * Time.deltaTime); 
    }
}
+4

All Articles