Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 PlayCreatively · Sep 25, 2016 at 11:22 PM · inputmultiplayerclassesplayers

How do I control which class I'm accessing?

I'm making a couch co-op game. This is my first time attempting multiplayer so this should be an easy question.

I've got a script called InputManager. Inside of InputManager are 2 public static classes: Player1 and Player2. Inside of those classes are variables associated to the corresponding controller.

This game also contains vehicles. Each vehicle has a script that controls its movement and this is how they would access variables from the InputManager: InputManager.Player1.jump or InputManager.Player2.jump.

However my problem is, I only want to read the input from the player that is currently driving the vehicle, something like this: curPlayer.jump

How do I do this?

Comment
Add comment · Show 1
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 PlayCreatively · Sep 26, 2016 at 05:45 PM 0
Share

Can no one give me a simple solution to this problem?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Sep 26, 2016 at 12:47 AM

You can't with static classes.

That's the reason why we actually have instances of classes, so we can reference them. What's actually the difference between Player1 and Player2? Usually you would have a Player class and then have an instance for player1 and one for player2. If the code for those should be different you could create seperate subclasses for each and create one instance for each player.

Can you post some actual code how your Player1 / 2 class looks like? Do they even contain methods / properties or only fields?

Comment
Add comment · Show 2 · 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 PlayCreatively · Sep 26, 2016 at 01:05 AM 0
Share
 public class Input$$anonymous$$anager : $$anonymous$$onoBehaviour {
 
     void Update ()
     {
         Player1.ReadInput();
 
         if (Player1.restart)
             Scene$$anonymous$$anager.LoadScene(0);
         if (Player1.Quit)
             Application.Quit();
 
         Player2.ReadInput();
     }
 
     public static class Player1
     {
         public static Vector2 dir;
         public static float Thrust;
         public static float zoom;
         public static bool restart, Quit;
 
         public static void ReadInput()
         {
             dir.x = Input.GetAxis("Horizontal1");
             dir.y = Input.GetAxis("Vertical1");
             if (dir.magnitude > 1)
                 dir.Normalize();
 
             zoom = Input.GetAxis("Zoom1");
             Thrust = Input.GetAxis("Thrust1");
             Thrust = (Thrust + 1) / 2;
 
             //Player1 privilege only!
             restart = Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Joystick1Button3);
             Quit = Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Joystick1Button9);
         }
     }
     public static class Player2
     {
         public static Vector2 dir;
         public static float Thrust;
         public static float zoom;
         public static bool leave;
 
         public static void ReadInput()
         {
             dir.x = Input.GetAxis("Horizontal2");
             dir.y = Input.GetAxis("Vertical2");
             if (dir.magnitude > 1)
                 dir.Normalize();
 
             zoom = Input.GetAxis("Zoom2");
             Thrust = Input.GetAxis("Thrust2");
             Thrust = (Thrust + 1) / 2;
 
             //Player2 privilege only!
             leave = false;
         }
     }
 }
 
avatar image PlayCreatively · Sep 26, 2016 at 01:51 AM 0
Share

So ins$$anonymous$$d of having to do this:

  if(isPlayer1)
          Input$$anonymous$$anager.Player1.jump

  else if(isPlayer2)
         Input$$anonymous$$anager.Player2.jump

I would do something like this:

 if(isPlayer1)
       curPlayer = Input$$anonymous$$anager.Player1;

 else if(isPlayer2)
       curPlayer = Input$$anonymous$$anager.Player2;
 
 curPlayer.jump;

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

87 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

Related Questions

Recognize number of players and set differents input for an action 0 Answers

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

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

Is Unity able to utilize 10 gamepads at once? 2 Answers

Multiplayer Estruture 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