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 Nakor · Oct 07, 2015 at 09:39 PM · c#networkingspawningrpcgeneration

Custom Object Visibility (UNet)

I have created a test project to work in and create (learn) ways to do the things that my main project will need. Currently I am working on object visibility (showing/hiding based on proximity).

I have an enemy prefab set up with child transforms to hold one GameObject each, which is also a registered prefab. Each Enemy is randomly generated and comprised of 3 visible parts. Here is the relevant code:

 public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
     {
         // Instantiate prefab.
         GameObject player = (GameObject)Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
 
         player.transform.position = startPositions.GetRandom().position;
 
         // Spawn ship parts.
         ShipInfo theShip = player.GetComponent<ShipInfo>();
 
         playerShips.Add(theShip);
 
         // Add the player.
         NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
 
         NetworkInstanceId theShipID = theShip.gameObject.GetComponent<NetworkIdentity>().netId;
         
 
         // FRONT
         GameObject objToSpawn = shipParts.GetRandom();
         GameObject newFrontObj = GameObject.Instantiate(objToSpawn);
         theShip.SetFrontObj(newFrontObj);
         NetworkServer.Spawn(newFrontObj);
         NetworkInstanceId frontPartID = newFrontObj.GetComponent<NetworkIdentity>().netId;
         ClientScene.objects[frontPartID].transform.parent = ClientScene.objects[theShipID].transform.FindChild("Front").transform;
         newFrontObj.transform.localPosition = Vector3.zero;
         newFrontObj.transform.rotation = Quaternion.identity;
 
         // BASE
         objToSpawn = shipParts.GetRandom();
         GameObject newBaseObj = GameObject.Instantiate(objToSpawn);
         theShip.SetBaseObj(newBaseObj);
         NetworkServer.Spawn(newBaseObj);
         NetworkInstanceId basePartID = newBaseObj.GetComponent<NetworkIdentity>().netId;
         ClientScene.objects[basePartID].transform.parent = ClientScene.objects[theShipID].transform.FindChild("Base").transform;
         newBaseObj.transform.localPosition = Vector3.zero;
         newBaseObj.transform.rotation = Quaternion.identity;
 
         // REAR
         objToSpawn = shipParts.GetRandom();
         GameObject newRearObj = GameObject.Instantiate(objToSpawn);
         theShip.SetRearObj(newRearObj);
         NetworkServer.Spawn(newRearObj);
         NetworkInstanceId rearPartID = newRearObj.GetComponent<NetworkIdentity>().netId;
         ClientScene.objects[rearPartID].transform.parent = ClientScene.objects[theShipID].transform.FindChild("Rear").transform;
         newRearObj.transform.localPosition = Vector3.zero;
         newRearObj.transform.rotation = Quaternion.identity;
 
         Debug.Log("Player added with ID:  " + conn.connectionId);
         
         int playerConnectionID = theShip.connectionToClient.connectionId;
 
         if (playerConnectionID == -1)
         {
             playerConnectionID = 1;
         }
         else if (playerConnectionID > 0)
         {
             playerConnectionID++;
         }
 
         theShip.CmdSetTagObjectName("Player" + playerConnectionID);
 
         newFrontObj.name = "frontPart";
         newFrontObj.GetComponent<ParentTagSystem>()
             .CmdSetupObjectName("Player" + playerConnectionID, PartType.Front);
 
         newBaseObj.name = "basePart";
         newBaseObj.GetComponent<ParentTagSystem>()
             .CmdSetupObjectName("Player" + playerConnectionID, PartType.Base);
 
         newRearObj.name = "rearPart";
         newRearObj.GetComponent<ParentTagSystem>()
             .CmdSetupObjectName("Player" + playerConnectionID, PartType.Rear);
     }

This works fine. The problem is with the proximity checker. When it notices a player is far enough away it destroys the enemy object by default. I'm not sure how to customize this behaviour. I've been looking through the documentation for a while now and I'm not getting it. It is possible I am just way too tired right now so hopefully someone can simply point me in the right direction.

What I probably want to have happen is actual disabling (unless there is a better option) of the objects rather than destroying of them. Alternatively I would have the server somehow tell the client how to reconstruct the correct enemy GameObject. This is probably not that difficult at all, I'm just not seeing how to control it. I imagine this would be a RPC type situation.

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

34 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

Related Questions

Parameters for RPC calls/Commands 1 Answer

How can i spawn my player in matchmaker using my UI? 0 Answers

Acess server stuff by the client 0 Answers

Unity Networking RPC for new connection 0 Answers

how to properly clean up the NetworkView to prevent errors ? 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