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 /
  • Help Room /
avatar image
0
Question by toomasio · Sep 01, 2016 at 02:36 AM · unity 5networkingspawn

UNET 5.4 - Spawn an object from client...and move it around from client....?

I have tried [Commands] to [ClientRpc]s...and I have also tried NetworkServer.SpawnWithClientAuthority. The most success I have had is spawn an object on the server side...and it also spawns double on client side. I am assuming because the spawn is sent twice by the same player on both client and server. The client is still unable to move the object though.

Does anybody have a code example of how to spawn an object from a client, have it appear on both server and all clients properly?

I am trying to make it so my CoOp players can build towers on the same level....and so that the players can see a preview of the tower moving around on the map before it is placed.

it works perfectly fine locally...just cant get this to work over Unet.

Here is some of my code:

     if (spawn && building == false)
     {
         RaycastSpawnPoint();
     }

     //placing tower
     if (building)
     {
         PositonTower();
         build = true;
     }

 }
 
 void RaycastSpawnPoint()
 {
     Ray spawnRay = playerCamera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
     RaycastHit spawnRayHit;

     if (Physics.Raycast(spawnRay, out spawnRayHit))
     {
        CmdSpawnTower(towerSpawn);
     }
 }
   

 [Command]
 void CmdSpawnTower(GameObject obj)
 {
       //Do I need an isServer here?
       //how do I send authority back to client that spawns this?
      // Do I need an Rpc? I have Tried this but I still get no authority.
         GameObject tempObj = (GameObject)Instantiate(obj);
         NetworkServer.SpawnWithClientAuthority(tempObj, connectionToClient);
     
         spawnedTower = tempObj;
         childEmpty = (GameObject)Instantiate(gridHelper);
         Debug.Log("Spawning " + childEmpty.name);

         Debug.Log("Spawning " + spawnedTower.name);
         Collider spawnCol = spawnedTower.GetComponent<BoxCollider>();
         spawnCol.enabled = false;

         building = true;
         
 }

All of my prefabs are in the network manager spawnables and they all have networkIdentity attached set to "local authority".

any help is appreciated!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by toomasio · Sep 01, 2016 at 05:58 AM

I figured it out

using UnityEngine; using UnityEngine.Networking; using System.Collections;

public class PlayerBuild : NetworkBehaviour {

 [SerializeField]
 private TowerManager towerManager;
 [SerializeField]
 private float gridSnap = 1;
 [SerializeField]
 private GameObject gridHelper;
 [SerializeField]
 private LayerMask buildMask;
 [SerializeField]
 private Camera playerCamera;

 public bool build = false;
 private bool spawn = false;

 private bool building;

 private GameObject spawnedTower;
 private GameObject childEmpty;

 private Vector3 childPos;
 private Vector3 gridScale;

 private GameObject towerSpawn;
 private TowerItem towerItem;
 private bool towerSwitch = false;

 public NetworkHash128 assetId { get; set; }

 public delegate GameObject SpawnDelegate(Vector3 position, NetworkHash128 assetId);
 public delegate void UnSpawnDelegate(GameObject spawned);

 // Use this for initialization
 void Start () {

     ClientScene.RegisterSpawnHandler(assetId, Spawn, UnSpawn);
     
     //Assign First Tower to tower spawn
     towerSpawn = towerManager.GetTower(1).GetPrefab();
     towerManager.SetTowerUIChosenTower(1);

 }
 
 // Update is called once per frame
 void Update () {

     if (!isLocalPlayer)
     {
         return;
     }

     //loop through all quick commands
     for (int i = 1; i <=8 ; i++)
     {
         if (Input.GetButtonDown("Quick" + i))
         {
             
             CmdSwitchTower(i);
             towerSwitch = true;
             towerManager.SetTowerUIChosenTower(i);
         }
     }
     
     //spawn tower
     if (Input.GetButtonDown("Build"))
     {
         spawn = !spawn;
         towerSwitch = false;

         if (spawn == false)
         {
             CmdUnspawnCurTower();
             build = false;
             building = false;
         }
     }

     if (spawn && building == false)
     {
         RaycastSpawnPoint();
     }

     //placing tower
     if (building && childEmpty != null)
     {
         PositonTower();
         build = true;
     }

 }

 [Command]
 void CmdSwitchTower(int i)
 {
     towerItem = towerManager.GetTower(i);
     towerSpawn = towerItem.GetPrefab();
     
 }

 [Command]
 void CmdUnspawnCurTower()
 {
     if (spawnedTower != null && childEmpty != null)
     {
         Destroy(childEmpty);
         Debug.Log(childEmpty.name + " was destroyed");

         UnSpawn(spawnedTower);
         NetworkServer.UnSpawn(spawnedTower);
        
         Debug.Log(spawnedTower.name + " was destroyed");
     }
     else
     {
         Debug.Log("could not unspawn tower");
     }

 }

 GameObject Spawn(Vector3 pos, NetworkHash128 assetId)
 {
     return (GameObject)Instantiate(towerSpawn, pos, Quaternion.identity);
 }

 void UnSpawn(GameObject spawned)
 {
     Destroy(spawned);
 }


 void RaycastSpawnPoint()
 {
     Ray spawnRay = playerCamera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
     RaycastHit spawnRayHit;

     if (Physics.Raycast(spawnRay, out spawnRayHit))
     {
         assetId = NetworkHash128.Parse("e2656f");
         CmdSpawnTower(spawnRayHit.point);
         building = true;
     }
 }

 [Command]
 void CmdSpawnTower(Vector3 pos)
 {
     
         spawnedTower = Spawn(pos, assetId);
         NetworkServer.SpawnWithClientAuthority(spawnedTower, connectionToClient);
         Debug.Log("Spawning " + spawnedTower.name);

         Collider spawnCol = spawnedTower.GetComponent<BoxCollider>();
         spawnCol.enabled = false;

     RpcSetSpawnValues(spawnedTower);
           
 }
 [ClientRpc]
 void RpcSetSpawnValues(GameObject spawned)
 {
     spawnedTower = spawned;

     childEmpty = (GameObject)Instantiate(gridHelper);
     Debug.Log("Spawning " + childEmpty.name);
 }

 void PositonTower()
 {

     Ray buildRay = playerCamera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
     RaycastHit buildRayHit;

     if (Physics.Raycast(buildRay, out buildRayHit, Mathf.Infinity, buildMask))
     {
         Collider buildHitCol = buildRayHit.collider;
         childEmpty.transform.SetParent(buildRayHit.collider.gameObject.transform);
         childPos = buildHitCol.transform.InverseTransformPoint(buildRayHit.point);

         //get tower floor scale
         gridScale = buildHitCol.transform.lossyScale;

         // snap to 1 meter (or your choosing) based on floor size
         float gridSnapX = gridSnap / gridScale.x;
         float gridSnapZ = gridSnap / gridScale.z;

         // snap to 1 meter using Rounding
         float snapX = Mathf.Round(childPos.x / gridSnapX) * gridSnapX;
         float snapZ = Mathf.Round(childPos.z / gridSnapZ) * gridSnapZ;

         childEmpty.transform.localPosition = new Vector3(snapX, 0, snapZ);

         //tower properties
         spawnedTower.transform.rotation = Quaternion.FromToRotation(Vector3.up, buildRayHit.normal);
         spawnedTower.transform.position = childEmpty.transform.position;

     }

     if (towerSwitch)
     {
         CmdUnspawnCurTower();
         RaycastSpawnPoint();
         towerSwitch = false;
     }

 }

}

Comment
Add comment · Show 1 · Share
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
avatar image Madrig88 · Sep 05, 2016 at 05:24 PM 0
Share

I am having a relatively similar issue and I cannot understand your fix. Because your full code is not shown in your original post, its hard to make a comparison as to everything you changed. What did you do to fix your problem?

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

121 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 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

[UNET] Can't spawn players with code 1 Answer

NetworkServer.Spawn() only on Server (with registered prefabs) [Unity 5.2.3f1] 0 Answers

unet synchroinze simple procedurally created map on clients 0 Answers

Cannot Spawn networkbehaviour gameobject 0 Answers

UNET | How to use network messages? 1 Answer


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