Home > database >  Unity 3D Input.GetAxis("Horizontal"); not working
Unity 3D Input.GetAxis("Horizontal"); not working

Time:07-24

(I am using Unity 2021.3.5 (latest), I wrote a script in C# for player movement, Horizontal input (A, D) movement works just fine but vertical input (W, S) is not working can someone help me?enter image description here

Code:

CodePudding user response:

You assigned the same variable name moveDirection for both vertical and horizontal directions, do not do that. Instead, assign horizontal input and vertical input to two different variables, say moveDirectionX and moveDirectionY. Adding both of these vectors gives the result.

Vector3 moveDirectionX, moveDirectionY;

public void MovePlayer()
{
    moveDirectionX = orientation.forward * verticalInput;
    moveDirectionY = orientation.right * horizontalInput;

    RB.AddForce((moveDirectionX   moveDirectionY).normalized * moveSpeed * 10f, ForceMode.Force);
}

CodePudding user response:

In Unity window go to Edit -> Project Settings -> Input Manager -> Axes -> Vertical.

You can change your input buttons in Negative Button and Positive button field or you can add alternative input: Alt Negative Button and Alt Positive Button.

Input Manager

But as mentioned from above you won't be able to move in both directions simultaneously due to assigning direction to one variable.

  • Related