- Home /
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.
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.
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.
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
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.
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:
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?
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
![](https://koobas.hobune.stream/wayback/20220613192054im_/https://answers.unity.com/themes/thub/images/avi.jpg)