- Home /
c#, RPC, Networking, Multiplayer Variables need 2 sync properly.
I have a few scripts that i have made using tutorials and such. They work together to create an online game with blocks as each player and a working chat, the blocks need health, i created a variable inside the playerscript.cs and placed it on the blocks so that they each have 100 hp, i created a button that when pressed the health is increased by one. but the health does not sync properly. if player 1 adds 1 hp, player 2 gains 1 hp also, and on the clients of others nobody got any hp not even player 1. i used rpc to track the playerHp variable but its not working, i think rpc is for giving a message or setting new values rather than tracking and comparing existing ones.. so im not sure how to do this and wondered if anyone could offer some advice.
problem: if i press button, player1 hp= playerhp+1 AND player2 hp = player2 hp+1 question: whats a good method to update health values and tell the other clients without changing their health values? i just want to "read" what my friends health values are, and see if they have levelled up achieved bigger health values. sorry for the long post, thanks for reading it.
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
public float speed = 10f;
public float playerHp = 100;
private float lastSynchronizationTime = 0f;
private float syncDelay = 0f;
private float syncTime = 0f;
private Vector3 syncStartPosition = Vector3.zero;
private Vector3 syncEndPosition = Vector3.zero;
private Quaternion syncStartRotation = Quaternion.identity;
private Quaternion syncEndRotation = Quaternion.identity;
private GameObject Target = null;
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
Vector3 syncPosition = Vector3.zero;
Vector3 syncVelocity = Vector3.zero;
float syncPlayerHp = 100;
Quaternion syncRotation = Quaternion.identity;
if (stream.isWriting)
{
syncPosition = rigidbody.position;
stream.Serialize(ref syncPosition);
syncPosition = rigidbody.velocity;
stream.Serialize(ref syncVelocity);
syncRotation = rigidbody.rotation;
stream.Serialize(ref syncRotation);
syncPlayerHp = playerHp;
stream.Serialize (ref syncPlayerHp);
}
else
{
stream.Serialize(ref syncPosition);
stream.Serialize(ref syncVelocity);
stream.Serialize(ref syncRotation);
stream.Serialize (ref syncPlayerHp);
syncTime = 0f;
syncDelay = Time.time - lastSynchronizationTime;
lastSynchronizationTime = Time.time;
syncEndPosition = syncPosition + syncVelocity * syncDelay;
syncStartPosition = rigidbody.position;
syncEndRotation = syncRotation;
syncStartRotation = rigidbody.rotation;
}
}
void Awake()
{
lastSynchronizationTime = Time.time;
}
void Update()
{
if (networkView.isMine)
{
InputMovement();
InputColorChange();
}
else
{
SyncedMovement();
}
if (Input.GetKeyDown(KeyCode.F)){
this.playerHp = playerHp + 1;
}
}
private void InputMovement()
{
if (Input.GetKey (KeyCode.W))
transform.Translate (Vector3.forward * 5 * Time.deltaTime);
if (Input.GetKey(KeyCode.S))
transform.Translate (Vector3.back * 5 * Time.deltaTime);
if (Input.GetKey(KeyCode.E))
transform.Translate (Vector3.right * 5 * Time.deltaTime);
if (Input.GetKey(KeyCode.Q))
transform.Translate (Vector3.left * 5 * Time.deltaTime);
if (Input.GetKey(KeyCode.A))
transform.Rotate (Vector3.up * -80 * Time.deltaTime);
if (Input.GetKey(KeyCode.D))
transform.Rotate (Vector3.up * 80 * Time.deltaTime);
if (Input.GetKey (KeyCode.Space))
rigidbody.AddForce(Vector3.up * 10);
}
private void SyncedMovement()
{
syncTime += Time.deltaTime;
rigidbody.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
rigidbody.rotation = Quaternion.Slerp(syncStartRotation, syncEndRotation, syncTime / syncDelay);
}
private void InputColorChange()
{
if (Input.GetKeyDown(KeyCode.R))
ChangeColorTo(new Vector3(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f)));
}
[RPC] void ChangeColorTo(Vector3 color)
{
renderer.material.color = new Color(color.x, color.y, color.z, 1f);
if (networkView.isMine)
networkView.RPC("ChangeColorTo", RPCMode.OthersBuffered, color);
}
void OnGUI(){
GUI.Box (new Rect (Screen.width - 100,Screen.height - 20, 100, 20), "Hp: " + playerHp);
if(Target != null){
GUI.Box (new Rect(Screen.width/2 - 50, 0, 300, 20), Target.name + " Hp: " + playerHp);
}
}
void OnMouseEnter(){
Target = this.gameObject;
}
void OnMouseExit(){
Target = null;
}
}
Answer by Punkjim420 · Jan 28, 2014 at 06:01 AM
i found the problem, it was the way i added hp and i missed something in my else statement at around line 36 i needed:
playerHp = syncPlayerHp;
Your answer
![](https://koobas.hobune.stream/wayback/20220613131900im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[Error] RPC function undefined in IRRELEVANT file? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers