Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 iR3dyPlayZ · Mar 13, 2021 at 07:20 AM · 2dinputkeyboardgamepad

Switching between Inputs

I'm trying to wrap my head around the unity input system, and I've figured out how to directly use inputs, but Im wanting to indirectly use them via the action map. but in doing so I've come to a big hole in the road, how do I switch between left stick input and wasd? because using left stick i use readvalue and I'm assuming for wasd ill be using getaxis Raw/ Get axis if that works... I'm honestly overwhelmed with all this and I just need an explanation and a brief demo on this so I can continue working on my "game"... it's taken me 4 days to get the perfect moving scripts imo but i didnt use the input system now i want to convert so i can use a game pad.

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
1
Best Answer

Answer by Ermiq · Mar 13, 2021 at 10:42 AM

That's where inheritance comes to help us.
You can create a base class that won't use any input methods and will just work with already evaluated and prepared values and will send the values to the movement system, and a couple of classes derived from the base class that will get direct inputs.


I usually create a base class PlayerInputBase that has, for example, moveVector, rotationVector, isRunning true/false, etc.
And for WASD/mouse inputs I create a new class PlayerInputPC that is derived from PlayerInputBase that gets player inputs directly from Input.GetAxis() and such and applies the values to the appropriate fields of the base class.
Same for touch inputs, for example: PlayerInputsTouch that gets input values from the touch joystick and UI buttons and sends the same type of values to the base class methods.
And in the movement script you can use something like transform.position += PlayerInputBase.moveVector - you don't have to use the exact name of PlayerInputPC or PlayerInputTouch, the movement system only knows the input reader class as PlayerInputBase and it has all the fields ( moveVector, isRunning, etc.).


Simple code structure example:

 public class PlayerInputBase : MonoBehaviour
 {
     // This class shouldn't be attached to gameObjects.
     // For actual input reading use either PlayerInputPC
     // or PlayerInputGamepad scripts.
 
     // The fields that will be evaluated by either PC or
     // Gamepad class and the movement script will read these values.
     public float moveX;
     public float moveY;
     
     protected virtual void Update() { }
 }
 
 // For PC inputs:
 public class PlayerInputPC : PlayerInputBase
 {
     protected override void Update()
     {
         base.Update();
         // ^^ ^^ Never forget to call the base class method as well.
         // In this example the PlayerInputBase.Update() does nothing,
         // but if it were doing something we would
         // certainly need the Update() in base class be executed 
 
         // Get inputs as PC inputs and store them as moveX and moveY
        // This class inherits these fields from the base class
         moveX = Input.GetAxis("Mouse X");
         moveY = Input.GetAxis("Mouse Y");
     }
 }
 
 // For gamepad inputs:
 public class PlayerInputGamepad : PlayerInputBase
 {
     protected override void Update()
     {
         base.Update();
         
         // I don't actually know how you read gamepad values
         // so here I use pseudocode
         moveX = Gamepad.current("X");
         moveY = Gamepad.current("Y");
     }
 }
 
 public class MovementScript : MonoBehaviour
 {
     // Here we don't care whether the actual input reading class is PlayerInputPC or PlayerInputGamepad.
     // In both cases it's also a PlayerInputBase as well, so we use it as just PlayerInputBase.
     PlayerInputBase playerInput;
     
     void Update()
     {
         Vector2 move = new Vector2(
             playerInput.moveX,
             playerInput.moveY);
         transform.position += move * Time.deltaTime;
      }
 }

Note:
This approach is designed in such way that you also should decide which input reading class you will use. Only one of them should be active in the scene (attached to some GameObject). Otherwise, one of them won't work, because the movement script will only read moveX, moveY from only one of the input classes.
For example, you can attach PlayerInputPC to one gameObject and PlayerInputGamepad to some other object, and depending on whether the player uses gamepad or not activate/deactivate the gameobject with the class that is needed/not needed right now.

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 iR3dyPlayZ · Mar 13, 2021 at 03:55 PM 0
Share

Thank you for this, I'm going over this answer now Nd I'm going to start working on it now I appreciate your help!

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

293 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Unity's 2D Gamekit Input problem 1 Answer

Unity input system only triggers once 2 Answers

Gamepad and keyboard act differently in multiplayer game using Input System 0 Answers

Multiple Players on one Computer/Console 5 Answers

How to find out if a button has been released (new input system/gamepad) 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