- Home /
Networking: RPC To Specific Player?
Hi, I'm new to networking and have got a game working where me and my friends can fly planes together. To add bullets and nametags, each plane needs to be identified. I have a few questions.
1) How do I speak specifically to one object, in everybody's game? Like if Plane A shoots Plane B, I want to send an RPC call to all clients (from the server) saying that Plane B is to take damage. The RPC call will trigger on every plane, so how do I specify a specific plane?
2) The planes have a networkview on their transform. They fly fairly smoothly but there is definite jitter. How can I make them fly smoothly? Prediction?
Thanks a ton!
I am in doubt myself for the jitter, but yeah I can answer first question. Why don't you try sending the networkView as an argument to the RPC call. and then, in the RPC function check if the received networkView is of the player B and then apply damage? this way, assume Player C, would find that its not its own networkView and it wont apply damage.
Hope this helps.
Answer by xt-xylophone · Feb 11, 2014 at 11:41 AM
For starters Ive only used Photon Network, not Unity's Networking but they are quite similar.
For 1, you can choose the Network Player of the the RPC, check the bottom: http://docs.unity3d.com/Documentation/ScriptReference/NetworkView.RPC.html
For 2, Yeah that will happen if its just the transform. What you want to do is make a custom script to sync the transform smoothly. Ive used this all the time:
using UnityEngine;
using System.Collections;
public class NetworkTransform : Photon.MonoBehaviour {
private Vector3 correctPlayerPos = Vector3.zero; // We lerp towards this
private Quaternion correctPlayerRot = Quaternion.identity; // We lerp towards this
void Update() {
if (!photonView.isMine) {
transform.position = Vector3.Lerp(transform.position, this.correctPlayerPos, Time.deltaTime * 5);
transform.rotation = Quaternion.Lerp(transform.rotation, this.correctPlayerRot, Time.deltaTime * 5);
}
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) {
if (stream.isWriting) {
// We own this player: send the others our data
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
} else {
// Network player, receive data
this.correctPlayerPos = (Vector3)stream.ReceiveNext();
this.correctPlayerRot = (Quaternion)stream.ReceiveNext();
}
}
}
What this does is it smoothly moves to the most recent rotation and position received as well as sending it out. Just make this what the network view is listening to. This is also for PhotonNetwork, so no copy+pasting, but hopefully it can put you on the right track!
I usually extend this so I can sync anything else needed. Its as simple as adding the variables in those 4 places. Good luck!