Hi
I am trying to create a network game. I have some trouble syncing the health of structures etc - newly logged on users don’t see the same as the server and other currently online players. Below is an example.
The server player logged in and did some damage to the cubes. Then the Player 1 logg in and the cubes health is set to the default value, and NOT to the current health.
BUT if either of the players do more damage, both see the same (and correct number).
I attaches this script to the cubes, and configure the amount of default health in the inspector.
#pragma strict
var HP:float;
private var localHP:float;
var explosionPrefab:GameObject;
private var prefabPosition:Transform;
function Start () {
//Debug.Log("HP "+ HP);
//Debug.Log("LHP " + localHP);
}
function takeDamage(damageVal:float){
Debug.Log(gameObject.name + " is taking "+ damageVal +" damage!");
HP -= damageVal;
if(HP <= 0){
Network.Destroy(gameObject);
Instantiate(explosionPrefab, transform.position, Quaternion.identity);
}
if(networkView.isMine) {
networkView.RPC("sendHPToServer", RPCMode.AllBuffered, HP, transform.position);
}
}
private var namePlatePos : Vector3;
var namePlate:GUIStyle;
var showHP:boolean;
function OnGUI(){
// Place the name plate where the gameObject is
if(showHP){
namePlatePos = Camera.main.WorldToScreenPoint(gameObject.transform.position);
GUI.BeginGroup(Rect((namePlatePos.x-50), (Screen.height - namePlatePos.y), 100, 50));
GUI.Label(Rect(0, 0, 100, 50), HP + "%",namePlate);
GUI.EndGroup();
}
}
function OnSerializeNetworkView(stream : BitStream, info : NetworkMessageInfo) {
if (stream.isWriting) {
var healthC : float = HP;
stream.Serialize(healthC);
} else {
var healthZ : float = 0;
stream.Serialize(healthZ);
HP = healthZ;
}
}
@RPC
function sendHPToServer(newHP:float, location:Vector3) {
HP = newHP;
//transform.position = location;
}
How can I make shure newly logged on players see the current damage done to the cubes.
I use on Network View to track the transforms of the cube. Can I use one to track variables?
I use this code on the Grenade object:
function explode() {
// Applies an explosion force to all nearby rigidbodies
var explosionPos : Vector3 = transform.position;
var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
for (var hit : Collider in colliders) {
if (hit && hit.rigidbody)
hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0);
hit.SendMessage("takeDamage",damage);
}
Destroy(gameObject);
Instantiate(explosionPrefab, transform.position, Quaternion.identity);
}
Please help me with this