- Home /
Using both Input.GetAxis and Input.GetAxisRaw
Hi all,
Currently, I am trying to use both GetAxisRaw (for ground movement) and GetAxis (for air movement) in my game. The reason I have configured it like this is because when I jump in my game with horizontal movement for example, I want the momentum to keep the player going.
If I use GetAxisRaw for this, the value changes from 1 to 0 instantly and the player stops moving as soon as I let go of the button.
The code is as follows:
if (controller.isGrounded)
{
horizontal = Input.GetAxisRaw("Horizontal");
vertical = Input.GetAxisRaw("Vertical");
}
else if (!controller.isGrounded)
{
horizontal += Input.GetAxis("Horizontal");
vertical += Input.GetAxis("Vertical");
}
This works fine and gives me the desired affect. The problem is when my player is grounded and starts running, if I'm holding down two keys (W + A for example), it appears as though the GetAxis value is increasing because if I let go of one of the keys (A in this case) and then jump, my player jump diagonally instead of forward.
I can see in the inspector that both horizontal and vertical values are at 1 or 0 in the inspector, but as soon as I jump, the value changes to a float range equal to how long I was holding the button, leading to diagonal jumping.
Does anyone know why the axis value is increasing even when I'm grounded? Or how I can prevent this problem? I hope I explained it well, let me know if I can provide any further information.
Regards Jason
Answer by JayFitz91 · Mar 30, 2021 at 08:40 PM
Just in case anyone was facing a similar issue, I found the following post on the unity forums which helped alot : https://forum.unity.com/threads/fps-character-controller-momentum-solution.894523/
With this, I was able to achieve my desired affect by removing the GetAxis call and storing air movement in a separate vector
//Checking for horizontal and vertical inputs
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
input = new Vector3(horizontal, 0.0f, vertical);
Vector3 forward = Vector3.Scale(mainCam.transform.forward, new Vector3(1, 0, 1)).normalized;
input = (vertical * forward + horizontal * mainCam.transform.right).normalized;
if(controller.isGrounded)
{
if (input.x != 0)
{
move.x += input.x * playerSpeed;
}
else
{
move.x = 0;
}
if(input.z != 0)
{
move.z += input.z * playerSpeed;
}
else
{
move.z = 0;
}
}
else
{
move.x += input.x * (playerSpeed / 2);
move.z += input.z * (playerSpeed / 2);
}
move = Vector3.ClampMagnitude(move, playerSpeed);
controller.Move(move * Time.deltaTime);
playerVelocity.y += gravityValue * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);
Cheers
Your answer
![](https://koobas.hobune.stream/wayback/20220613031459im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to move while jumping? 1 Answer
I can't do that my character jumps while running 0 Answers
Character Controller Jitters When Jumping Next To Objects 4 Answers
How do I make a jumping and ledge-grabbing system, similar to TLoZ OOT in unity 3d? 0 Answers
Character Controller jump doesn't work for some reason? 0 Answers