Hey there,
I am working on a FPS game and currently prototyping with basic assets to get the network stuff working.
Currently players do have a lobby to create rooms, join them etc. I am know creating some kind of library for objects which are usable in the world and here is where I’m stuck. I have got an object thats a health box, simple as it sounds, if I pick it up alone on the server or as the creator of the room, my script works fine and adds 20 health. If I am picking it up in my second app open, the client that joined the room later, it doubles the amount. Here are my scripts:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InteractableObject : MonoBehaviour {
public enum ObjectType { clip, health, door };
public enum DoorStatus { open, closed };
public ObjectType objectType;
public DoorStatus doorState;
public int amount;
public bool pickupable;
public Material mat;
PhotonView photonView;
void Start ()
{
photonView = GetComponent<PhotonView> ();
Debug.Log ("This is a: " + objectType);
}
void OnTriggerEnter(Collider collider)
{
if (pickupable && objectType == ObjectType.health) {
collider.transform.GetComponent<PlayerNetworkMover>().PickUpHealth(20f);
Kill ();
}
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
}
// Start the photon network call to all client clones
public void SendInteraction()
{
photonView.RPC ("ChangeMaterial", PhotonTargets.AllBuffered);
}
// Test PunRPC Call to try out interactable cross client changes
[PunRPC]
void ChangeMaterial()
{
GetComponent<MeshRenderer> ().material = mat;
}
[PunRPC]
void Kill()
{
if(GetComponent<PhotonView>().instantiationId == 0)
{
Destroy(gameObject);
}
else
{
if(GetComponent<PhotonView>().isMine == true)
{
PhotonNetwork.Destroy(gameObject);
}
}
}
}
and the script from the unity tutorial modified to add health:
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if(stream.isWriting)
{
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
stream.SendNext(health);
stream.SendNext (anim.GetBool("Aim"));
stream.SendNext (anim.GetBool("Sprint"));
}
else
{
position = (Vector3)stream.ReceiveNext();
rotation = (Quaternion)stream.ReceiveNext();
health = (float)stream.ReceiveNext();
aim = (bool)stream.ReceiveNext ();
sprint = (bool)stream.ReceiveNext ();
}
}
public void PickUpHealth(float amount)
{
health += amount;
if (health > 100)
health = 100;
UpdateHealth ();
}
[PunRPC]
public void GetShot(float damage, string enemyName)
{
health -= damage;
UpdateHealth ();
if(health <= 0 && photonView.isMine)
{
if (SendNetworkMessage != null)
SendNetworkMessage (PhotonNetwork.player.NickName + " was killed by " + enemyName);
if(RespawnMe != null)
RespawnMe(3f);initialLoad = true;
PhotonNetwork.Destroy (gameObject);
}
}
void UpdateHealth(){
healthBar.fillAmount = health / 100;
if (playerUI.activeSelf) {
healthUI.fillAmount = health / 100;
healthTextUI.text = health.ToString ();
}
}
thanks in advance for any hints! ![]()