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 BinaryAndroid · Dec 31, 2013 at 12:43 AM · 2 player

How to define a GameObject for both players?

Hello guys! I'm searching for a while on how it is supposed to work for a given object(which would be a character) to be controlled by 2 players.

The very same thing that happens in a fighting game where both players can select and control the same characte. I have seem something with the PreFab and the tag index (arrays) but I've been facing some problems ... it's ok when I code it very straight forward like:

 if(Input.GetKeyDown(KeyKode.A))//Player one 
 {
    player[0].rigidbody2D.velocity = new Vector2(2f, 0);
 }else 
 if(Input.GetKeyDown(KeyKode.B))// PLayer two
 {
    player[1].rigidbody2D.velocity = new Vector2(2f, 0);
 }

But the problem comes around when I try to put it into a more organized code like the following:

Note: this is found in a baseclass

public void Vel(float X, float Y) {

   if(player[0].tag == "Player")
   }
       player[0]. rigidbody2D.velocity = new Vector2(X, Y);
   }else
   if(player[1].tag == "Player")
   {
         player[1].rigidbody2D.velocity = new Vector2(X, Y);
   }

}

And then call the Vel method in the update method in a child class...

 if(player[0].tag == "Player" && Input.GetKeyDown(KeyCode.B))
 {
    Vel(1f, 0);
 }
 
 if(player[1].tag == "Player" && Input.GetKeyDown(KeyCode.N))
 {
    Vel(1f, 0);
 }

It always trigger the first "if" condition. No matter if I switch to player 2 code it always trigger only the first if. I want to know if there is a specific way to build a multiplayer(offline) controller detection.

Thanks in advance.

Comment
Add comment · Show 2
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 HuskyPanda213 · Dec 31, 2013 at 01:49 AM 0
Share

Check the else if statement, remove "else".

avatar image BinaryAndroid · Dec 31, 2013 at 02:51 AM 0
Share

I've done that already in fact remove else wouldn't help in this case. The problem is that both players get info from the same script. I just want to know the right way to build a solid multiplayer system.

Thank you!

0 Replies

· Add your reply
  • Sort: 

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

19 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

Related Questions

Finding the closest player and making an action happend when player is in range 1 Answer

count how many times a player has played 0 Answers

2 virtual joysticks controlling 2 different game objects ( players ) pls help 0 Answers

Fog for 1 of the 2 players 1 Answer

Second player not moving sucks in its place? 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