- Home /
Networking: multiple childs issue
basicly, we have 2 turrets on our game, wich are child of a main game object Who is the main body that holds there 2+ turrets.
problem is, we have both turrets movement sync perfectly and all
but we have an issue with unity mixing the turrets fire, we usually copy/paste the component on the main body "parent" gameObj for each turret on said parent obj.
This leads to some weird behaviour, Unity only fires projectiles from the first turret, all our copies (of this script as a component) below the first one, are spawning their proyectiles on the first turret's child.
Somehow we realize unity reads components from top to bottom on the inspector, and it thinks the first turret is the only existing one, cause all copies of this script only fire from the first turret position/child for some reason.
Any clue/help advice on this topic? we kind of new to unity networking.
-And we stuck with Unet on Unity 2018.2.14f1 build cause we heard latest versions of Unity Removed Unet and are awaiting for a new kind of networking meme "Maybe to be introduced next year" / "fixed-implemented properly 2021"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class Fire1 : NetworkBehaviour {
public Vector3 bulletoffset = new Vector3(0, 0.2f, 0);
public KeyCode key;
public GameObject bulletprefab;
public float firedelay = 10f;
public float lifespam = 2.5f;
public AudioClip ready;
public float soundvalue = 0.2f;
float cooldown = 0;
public int speed = 6;
public Rigidbody2D rb2d;
int speedy;
public GameObject turretobj;
void Start(){
Debug.Log(turretobj.transform.position);
}
// Update is called once per frame
void FixedUpdate(){
if (!isLocalPlayer) return;
float vely = (rb2d.transform.right.sqrMagnitude);
int vel = (int)vely;
speedy = vel;
if ( cooldown > 0) cooldown -= Time.deltaTime;
if (Input.GetKey(key) && (cooldown <= 0)) Fire();
}
void Fire (){
cooldown = firedelay;
CmdFire(TestClient.cid);
}
[Command]
void CmdFire (int cid){
Vector3 offset = turretobj.transform.rotation * bulletoffset;
Vector3 pos = turretobj.transform.position + offset;
Vector3 rot = turretobj.transform.rotation.eulerAngles;
FireInstance (pos, rot, cid);
RpcFire (pos, rot, cid);
}
[ClientRpc]
void RpcFire (Vector3 pos, Vector3 rot, int cid){
FireInstance (pos, rot, cid);
}
void FireInstance (Vector3 pos, Vector3 rot, int cid){
GameObject bullet = Instantiate(bulletprefab) as GameObject;
bullet.transform.position = pos;
bullet.transform.eulerAngles = rot;
bullet.GetComponent<Rigidbody2D>().velocity = bullet.transform.right * (speed + speedy);
bullet.GetComponent<Projectile>().damage.clientid = cid;
AudioSource.PlayClipAtPoint(ready, transform.position, soundvalue);
Destroy(bullet, lifespam);
}
}
Answer by GonTar_X · May 12, 2019 at 03:35 AM
anyone? we thought of adding IDs to every turret throught the script and did so, but no luck whatsoever