- Home /
How do you network an object to be a child of another object (so that it shows up in the hierarchy) in both the client and server?
How do you network an object to be a child of another object (so that it shows up in the hierarchy)?
Right now, I am making a game where octopus are shooting harpoons at each other. I am trying to make it multiplayer across a network using Unity's built in tools. Right now, I have a playerShoot script (C#) as a component of the octopus prefab:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.Networking;
public class playerShoot : NetworkBehaviour {
[SerializeField]
GameObject bulletPrefab;
[SerializeField]
Transform weaponTip;
GameObject octopus;
Rigidbody2D Temporary_rb;
Rigidbody2D octopus_rb;
public int fireRate; //Fire rate
public int damage; //Damage
int timeFire; // Fire rate counter
private void Awake()
{
fireRate = 50;
timeFire = 0;
octopus_rb = GetComponent<Rigidbody2D>();
octopus = GetComponent<GameObject>();
}
[Command]
public void CmdFire()
{
if (timeFire >= fireRate)
{
//Reset fire rate counter
timeFire = 0;
//Instantiating bullet
var Temporary_Bullet_Handler = Instantiate(bulletPrefab, weaponTip.position, transform.rotation);
Temporary_rb = Temporary_Bullet_Handler.GetComponent<Rigidbody2D>();
Temporary_rb.velocity = (Temporary_rb.position - octopus_rb.position);
//Spawning bullet
NetworkServer.Spawn(Temporary_Bullet_Handler);
}
}
private void Update()
{
timeFire += 1;
}
}
CmdFire is called whenever the player presses the down arrow. My goal for the projectile that is being shot (a harpoon) is for it to collide with another player's octopus, spawn an object that is a harmless copy of it (under the name of harpoonEffect ) slightly in the other octopus' body, and parent that sprite to the octopus it collided with so that the harpoon seems like it is lodged in the octopus' body.
This script is a component of the actual harpoon:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class BulletMovement : NetworkBehaviour {
public float timeToDestroy = 2f; //TIme until the bullet (not the bullet effect) is destroyed
public Rigidbody2D bullet;
public float velocityScale = 100.0f; //The scale for the speed of bullet
//The harmless harpoon image
[SerializeField]
GameObject harpoonEffect;
private void Start()
{
StartCoroutine(AutoDestroy(timeToDestroy));
bullet = GetComponent<Rigidbody2D>();
bullet.velocity *= velocityScale;
}
[Command]
public void CmdSpawnHarpoonEffect(GameObject collidingGO)
{
Vector3 v = collidingGO.GetComponent<Rigidbody2D>().velocity;
GameObject newGO = (GameObject)(Instantiate(harpoonEffect, transform.position, transform.rotation));
//Set server parent
NetworkInstanceId parentNetId = collidingGO.GetComponent<NetworkIdentity>().netId;
GameObject parentObject = NetworkServer.FindLocalObject(parentNetId);
newGO.transform.SetParent(parentObject.transform,true);
//Moving the harpoon inside the body
newGO.transform.position += v / 4;
NetworkServer.Spawn(newGO);
}
private void OnCollisionEnter2D(Collision2D collision)
{
foreach (ContactPoint2D contact in collision.contacts)
{
//Spawn blood at collision
}
if (collision.collider.gameObject.tag == "Player" || collision.collider.gameObject.tag == "Projectile")
CmdSpawnHarpoonEffect(collision.collider.gameObject);
NetworkServer.Destroy(gameObject);
}
IEnumerator AutoDestroy(float _time)
{
yield return new WaitForSeconds(_time);
Debug.Log("Destroying in autodestroy!");
NetworkServer.Destroy(gameObject);
}
}
The behavior of the instantiated & spawned harpoonEffect is odd. The object lags behind the shot ocotpus, but only in the client (not the server). The server has no lag whatsoever. The left monitor in the picture is the host and the right monitor is the client.
The hierarchy is correct with the harpoonEffect being nested in the correct octopus, but that's only the case for the server. In the client, the harpoonEffect is only in standard/highest level of the hierarchy. Is the lag from the lack of parenting, and if so, how do I fix it? Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220612143154im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Why can't the client move a networked object ? 1 Answer
connecting two people over the Internet 1 Answer
Server not receiving message? 1 Answer
Networking in a 2 player web game using a player as the server 0 Answers