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
1
Question by PancakesNBacon · Jun 18, 2015 at 09:45 PM · inputcharactercontrollercontrollercharacter controllerkeyboard input

How do I have multiple controls for the same function? (Controller and Keyboard)

I am working on a simple platformer game and so far the controls are working, but will only work for one set of controls at a time, either the Xbox One controller or the keyboard. I want the player to have the option to use either for moving left or right with the character.

But how do I set 2 different inputs to the same float?

With this current code, it still recognizes the controller input, but will only flip the sprite, while the keyboard will flip the sprite and move the character.

 float move = Input.GetAxis ("LeftJoystickX");
         GetComponent<Rigidbody2D>().velocity = new Vector2 (move * movementSpeed, GetComponent<Rigidbody2D>().velocity.y);
 
         if (move > 0 && !facingRight)
             Flip ();
         else if (move < 0 && facingRight)
             Flip ();
 
         float moveK = Input.GetAxis ("Horizontal");
         GetComponent<Rigidbody2D>().velocity = new Vector2 (moveK * movementSpeed2, GetComponent<Rigidbody2D>().velocity.y);
 
         if (moveK > 0 && !facingRight)
             Flip ();
         else if (moveK < 0 && facingRight)
             Flip ();

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

Answer by Shark-Boy · Jun 19, 2015 at 12:57 AM

You could use a if to check if you are getting input from the keyboard and if not then check the gamepad. It would look something like this.

 float move = 0f;
 float inputKeyboard = Input.GetAxis ("Horizontal");
 float inputGamepad = Input.GetAxis("LeftJoystickX");
 
 if (inputKeyboard > 0.1 && inputKeyboard < -0.1)
 {
    move = inputKeyboard;
 }
 else if (inputGamepad > 0.1 && inputGamepad < -0.1)
 {
    move = inputGamepad;
 }

Then use move for fliping the sprite and the rigidbody2d.velocity.

Also using GetComponent() every frame is hard on you computer so the standard practice is to create a variable called something like rigidbody2d (no caps) and in the start function set it to GetComponent like this.

 using System;
 // and all the other using stuff
 
 public class myClass : MonoBehaviour {
    
    Rigidbody2D rigidbody2d;
 
    void Start()
    {
       rigidbody2d = GetComponent<Rigidbody2D>();
    }
 }

Then you can use that variable to set the velocity like this.

 rigidbody2d.velocity = // something

Hope this helps!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Code for - Rigidbody vs Character Controller 0 Answers

Player inversed inputs 0 Answers

Gradual Speed Increase with Variable Input 0 Answers

how can I assign and use different controller schemes for a single keyboard? 0 Answers

Getting analog input values from multiple controllers 2 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