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 UFO_505 · Apr 03, 2015 at 02:30 PM · networkonlinenetworkviewonline game

How to send input in an online game?

I'm trying to send each player's input to the other players:

 float verticalAxis = 0;
 
 void Update () {
     readWS();
 }

 [RPC]
 public void sendWS(float VerticalAxis)
 {
     VerticalAxis = Input.GetAxisRaw("Vertical");
 }

 public void readWS()
 {
     if(networkView.isMine != true)
     {
         networkView.RPC ("sendWS", RPCMode.All, verticalAxis);

         Debug.Log(verticalAxis);
     }
 }

Could someone please tell me why this doesn't work?

Before the second player joins the server, the debug doesn't show anything. After the second player joins, it reads "0". So it seems that the verticalAxis value isn't being updated.

I've tested and Input.GetAxisRaw("Vertical") does give a correct value.

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 Lilbob · Apr 03, 2015 at 05:03 PM 0
Share

I dont quite understand what you're doing but I can tell you whats happening. you're sending verticleAxis, which never gets changed. your RPC function changes VerticleAxis. verticleAxis will always be zero because it never changes.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hbalint1 · Apr 03, 2015 at 06:11 PM

There are some major problems. The first one is what Lilbob mentioned. Let's try this:

  float verticalAxis;
 
  void Update()
  {
      readWS();
  }
 
  [RPC]
  public void sendWS(float VerticalAxis)
  {
      // here everyone will execute the code because of AllBuffered
      // so simply change the value of the players with the new one
 
      transform.Rotate(0, VerticalAxis, 0); // I'm not sure how to do this properly :D
         
  }
 
  public void readWS()
  {
      verticalAxis = Input.GetAxisRaw("Vertical");   // read my axis value to send to the others
 
      if(networkView.isMine) // if I'm the locally controlled player then...
      {
          // ...I tell everyone in the room to execute the sendWS function
          // AllBuffered, because if someone joins later, he will get the
          // new data immediately from the server
          networkView.RPC ("sendWS", RPCMode.AllBuffered, verticalAxis);
      }
  }

The code is untested, so maybe it doesn't work.

Comment
Add comment · Show 5 · 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 UFO_505 · Apr 03, 2015 at 11:31 PM 0
Share

I'm afraid that doesn't work either. It just turns the player that makes the input.

but thanks a lot for the help so far guys, really appreciate

avatar image UFO_505 · Apr 04, 2015 at 10:36 AM 0
Share

Okay, I've got it to work and I basically just used a variation of your code, hbalint1 so I'm not really sure why yours didn't work, but anyway, here's the working version:

     void Update()
     {
         sendWS();
     }
     
     [RPC]
     public void readWS(float VerticalAxis)
     {
         Debug.Log(VerticalAxis);
     }
     
     public void sendWS()
     {
         if(networkView.is$$anonymous$$ine)
         {
             networkView.RPC ("sendWS", RPC$$anonymous$$ode.Others, Input.GetAxisRaw("Vertical"));
         }
     }

(I switched the names of 'sendWS' and 'readWS' because they didn't really make sense.)

Thanks a lot to everyone for the help.

avatar image hbalint1 · Apr 04, 2015 at 11:52 AM 0
Share

I'm glad I could help :) But i have one remark. You should use RPC only when something changes rarely on your network. For example I make a Prop Hunt game and when I change my appearance I use RPC. If youwant to keep synced an objects position, you should use the OnPhotonSerializeView ins$$anonymous$$d. So I recommend you to give you object a PhotonView component and in the observed component drag and drop the objects Transform (from the inspector). Change the observe option to reliable. With this you can keep synced the transform frame by frame. I think you should read this:

http://www.paladinstudios.com/2014/05/08/how-to-create-an-online-multiplayer-game-with-photon-unity-networking/

avatar image UFO_505 · Apr 05, 2015 at 10:01 AM 0
Share

That photon thing looks really good, I'll probably switch to it straight away. Could I also ask why I shouldn't use RPC on things that change constantly?

avatar image hbalint1 · Apr 05, 2015 at 06:16 PM 0
Share

Sorry. I don't know the answer to this. maybe:

http://answers.unity3d.com/questions/175728/when-to-use-onserializenetworkview-and-rpcs.html

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I create an online object counter 2 Answers

Parenting over network 0 Answers

How to let the other network player to see a launched rocket and its explosion? 1 Answer

How to use an RPC to send an animation over the network? 2 Answers

Photon Unity Networking fix physics delay 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