- Home /
Photon Networking Child Inconsistencies
I am new to Photon networking and I've been struggling with making a simple multiplayer spaceship battles game. My main point of difficulty right now is the script to fire a projectile and have that projectile be present in all clients and be destroyed after a time period or after hitting something. My first error was with PhotonNetwork.Destroy()
as I'd placed this script on the projectile with a photonView and was told that it cannot perform network-remove because it is not owned by a client. To counter this I made all projectiles fired children of the player object that fired them. In the editor, this works perfectly, but when another client is in the same world, the bullets start moving and rotating with the player as a normal child would. Why the inconsistency?
This script is attached to my player:
using UnityEngine;
using System.Collections;
public class Shoot : Photon.MonoBehaviour {
public Rigidbody Projectile;
public int Speed = 500;
public Transform bulletSpawn;
void Start () {
PhotonNetwork.AllocateViewID ();
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Space)) {
photonView.RPC("InstantiateBullet", PhotonTargets.All);
}
}
[RPC]
void InstantiateBullet () {
Rigidbody clone = (Rigidbody)Instantiate(Projectile, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
clone.gameObject.transform.parent = gameObject.transform;
}
}
And this script to the projectile:
using UnityEngine;
using System.Collections;
public class Bullet : Photon.MonoBehaviour {
public int damage = 10;
public int Speed = 500;
public int tempTimer = 10;
void Start () {
transform.parent.rigidbody.velocity = transform.TransformDirection(Vector3.up * Speed);
StartCoroutine (Wait ());
//PhotonNetwork.AllocateViewID ();
}
void OnTriggerEnter (Collider other) {
if (other.gameObject.tag == "Player") {
Health Script = other.GetComponent<Health> ();
Script.health -= damage;
print ("hit");
}
photonView.RPC ("BulletDestroy", PhotonTargets.All);
}
IEnumerator Wait () {
yield return new WaitForSeconds(tempTimer);
photonView.RPC ("BulletDestroy", PhotonTargets.All);
}
[RPC]
void BulletDestroy () {
Destroy (transform.parent.gameObject);
}
}
Do you still need this answered or will I be resurrecting the dead?
Your answer
Follow this Question
Related Questions
Photon View - Player Names/Properties. 1 Answer
Photon Unity Player Kill Counter 2 Answers
Photonnetwork check when somebody connects/disconnects 2 Answers
Matchmaking multiplayer help 0 Answers