Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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 BetaFruit · Nov 17, 2021 at 01:36 AM · inputaxisbooleanchangeinputmanager

Change input axis values from code

I have a bool in my settings menu that is called inverted controls. This is because the arrow keys and WASD are both used for different things and some people might want to swap their functions in the game.

How can I change an input axis buttons from code and if that's not possible what is another way I could do it?

here is a simplified version of the code:

I want this to be (left arrow) and (right arrow) if the inverted controls bool is true

 if (Input.getaxis("Horizontal")) 
 {
     //move the player
 }

I want this to be (a) if the inverted controls bool is true

 if (Input.GetKey(KeyCode.LeftArrow))
 {
     //move the camera left
 }

I want this to be (d) if the inverted controls bool is true

 if (Input.GetKey(KeyCode.RightArrow))
 {
     //move the camera right
 }



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

2 Replies

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

Answer by Eno-Khaon · Nov 17, 2021 at 09:56 AM

I'm a little confused by this; the "simplified" version makes this significantly easier:

 float invertMultiplier = invertControls ? -1.0f : 1.0f;
 float playerInputH = Input.GetAxis("Horizontal") * invertMultiplier;
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
avatar image
0

Answer by BetaFruit · Nov 17, 2021 at 08:41 PM

I'm really stupid and I'm not quite sure how to add that in or what it would do. But basically, I'm trying to change the horizontal axis negative button from a to left arrow and the positive button from d to right arrow and two if statements checking for a press of the left or right arrow to a press of a or d.

Maybe there is a way to use alt buttons (alt negative button and alt positive button) to do this?

Comment
Add comment · Show 1 · 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
avatar image Eno-Khaon · Nov 17, 2021 at 09:14 PM 0
Share

Well, let's break a few aspects of this down, then. First, the difference(s) between movement schemes:

 // Using a different KeyCode per direction moved
 Vector2 playerInput = Vector2.zero;
 if(Input.GetKey(moveLeftKeyCode)) // e.g. KeyCode.LeftArrow
 {
     playerInput.x = -1f;
     // play animation for moving left
 }
 else if(Input.GetKey(moveRightKeyCode)) // e.g. KeyCode.RightArrow
 {
     playerInput.x = 1f;
     // play animation for moving right
 }
 // move player using playerInput, now that it's been obtained
 
 // --------------------------
 
 // Using an axis for movement, then adapting to it
 Vector2 playerInput = new Vector2(Input.GetAxis("Horizontal"), 0f);
 // apply movement using playerInput,
 // since X-axis ranges from -1 to 1
 if(!Mathf.Approximately(playerInput.x, 0f))
 {
     // play animation based on direction character is moving
 }


Essentially, these are just two different ways of doing the same thing: Gathering Input/movement using discrete key/button presses (Input.GetKey()) or building input and applying it regardless of its value (Input.GetAxis()).

In either case, I'm deliberately separating "playerInput" from its usage to demonstrate how it can still be utilized in either implementation. This also makes it easier to factor in control disruptions:

 // Slow player movement
 float slowPlayer = 0.5f;
 playerInput *= slowPlayer;
 
 // Hasten player movement
 float hastenPlayer = 1.5f;
 playerInput *= hastenPlayer;
 
 // Reverse player movement
 float invertPlayer = -1.0f;
 playerInput *= invertPlayer;

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

164 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 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

Reading 'not defined' Controller Axis Input? 0 Answers

Steelseries 3gc controller key mappings 0 Answers

Get list of Axes? 4 Answers

Button Axis with New Input System 0 Answers

Creating an input manager that supports axis 0 Answers


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