Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JayFitz91 · Mar 29, 2021 at 04:07 PM · charactercontrollerjumpinggetaxisaxes

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

129 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Jumping not always work 2 Answers

Why doesn't he jump as high after Idle as he does after Walking? 1 Answer

How would I get my character to be able to move while in the air after a jump? 1 Answer

I have a problem with my c# code to make my charaktercontroller jump. 1 Answer

How to instantiate on collision a Jump event with Standard FPS controller? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges