Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by IShouldGetASuit · Nov 22, 2017 at 05:51 PM · networkingserverparentclient

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.

alt text

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!

untitled.png (431.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

105 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges