Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Throw/spawn objects (Unet/Client problem)

Discussion in 'UNet' started by emulo2, Dec 10, 2015.

  1. emulo2

    emulo2

    Joined:
    Apr 15, 2015
    Posts:
    21
    Hey guys!

    [First problem is solved. But i have a new problem. Please look at post 4]

    I have a problem... If i throw something with one tracked controller, only the the host can throw visible objects. I tried to let the client spawn objects with:


    Code (CSharp):
    1. [Command]
    2.  
    3.   void Cmd_Shoot()
    4.  
    5.   {
    6.  
    7.   var go = Instantiate (prefab);
    8.  
    9.   NetworkServer.Spawn (go);
    10.  
    11.   go.transform.position = attachPoint.transform.position;
    12.  
    13.  
    14.  
    15.   joint = go.AddComponent<FixedJoint> ();
    16.  
    17.   joint.connectedBody = attachPoint;
    18.  
    19.   }
    As a client I now get the message:


    “NetworkServer is not active. Cannot spawn objects without an active server.”


    The tracked controller is a child of the player

    Player (NetID, Transform) -> Camera -> Tracked Controller (NetID)

    Every prefab is registered in the manager….

    The Script is on the controller because it is a tracked controller and so I cannot put everything on the player.
     
    Last edited: Dec 12, 2015
  2. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    1. Please use code tags.
    2. Cmd can't be called from child GameObjects. The NetworkIdentity is placed on the base GameObject, and Cmd's are sent through the NetworkIdentity. So the script where the Cmd is called must be on the base GameObject.
     
    emulo2 likes this.
  3. emulo2

    emulo2

    Joined:
    Apr 15, 2015
    Posts:
    21
    Thank you!
     
  4. emulo2

    emulo2

    Joined:
    Apr 15, 2015
    Posts:
    21
    I call it now from the player. I use [command] and the host can see the spawned object. But the spawned object did not work with the rest of the function. It works fine if you be the host. So you can spawn the object, it is attached to the controller and you can throw the object. If you are the client, the objects spawns and fall down…… here is the part of my script on the player:

    Code (CSharp):
    1.  
    2. public class PlayerShoot : NetworkBehaviour {
    3.  
    4.     private int damage = 25; //später entfernen
    5.  
    6.  
    7.     private const string PLAYER_TAG = "Player";
    8.     public PlayerWeapon weapon;
    9.  
    10.     [SerializeField]
    11.     private GameObject ViveController;
    12.  
    13.     [SerializeField]
    14.     private GameObject GunParticle;
    15.  
    16.     [SerializeField]
    17.     private GameObject ControllerRechts;
    18.  
    19.     SteamVR_TrackedObject trackedObj;
    20.  
    21.  
    22.     [SerializeField]
    23.     private LayerMask mask;
    24.  
    25.     public GameObject prefab;
    26.     public GameObject prefabclient;
    27.     public Rigidbody attachPoint;
    28.     public Rigidbody punkt2;
    29.     public Rigidbody Controller;
    30.    
    31.     FixedJoint joint;
    32.  
    33. .......
    34.  
    35.  
    36.  
    37.  
    38.  
    39.  
    40.   [Command]
    41.     public void Cmd_Shoot()
    42.     {
    43.         if (joint == null) {
    44.             var go = Instantiate (prefab);    
    45.             NetworkServer.Spawn (go);
    46.             go.transform.position = punkt2.transform.position;
    47.    
    48.  
    49.             joint = go.AddComponent<FixedJoint> ();
    50.             joint.connectedBody = attachPoint;
    51.         }
    52.     }
    53.  
    54.     [Command]
    55.     public void Cmd_Throw()
    56.     {
    57.         if (joint != null) {
    58.         var go = joint.gameObject;
    59.         var rigidbody = go.GetComponent<Rigidbody> ();
    60.         Object.DestroyImmediate (joint);
    61.         joint = null;
    62.         Object.Destroy (go, 15.0f);
    63.  
    64.         trackedObj = GameObject.FindWithTag("Controller").GetComponent<SteamVR_TrackedObject>();
    65.         var device = SteamVR_Controller.Input ((int)trackedObj.index);
    66.         var origin = trackedObj.origin ? trackedObj.origin : trackedObj.transform.parent;
    67.  
    68.     if (origin != null) {
    69.             rigidbody.velocity = origin.TransformVector (device.velocity * 2);
    70.             rigidbody.angularVelocity = origin.TransformVector (device.angularVelocity * 2);
    71.  
    72.    } else {
    73.             rigidbody.velocity = device.velocity;
    74.             rigidbody.angularVelocity = device.angularVelocity;
    75.         }
    76.  
    77.         rigidbody.maxAngularVelocity = rigidbody.angularVelocity.magnitude;
    78.         }}
     
  5. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    It's kind of hard to help because it's hard to understand what you're trying to achieve, but I think I get it.
    On your prefab, is "Local Player Authority" checked or unchecked?
     
  6. emulo2

    emulo2

    Joined:
    Apr 15, 2015
    Posts:
    21
    I have tried both. But it did not work as a client. The prefab spawned but not on the right position and not binded on the controller.

    Maybe this Information is important: all objects (attachpoint and so on) are childs of the player. Which information Do you need?

    Thanks for your help and time!!
     
  7. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    Well since you're spawning the joint on the server, it should NOT be marked as Local Player Authority...since when it's time to throw it, the server is the one throwing it. This line here:
    Code (CSharp):
    1. var device = SteamVR_Controller.Input ((int)trackedObj.index);
    Is getting the Input from the server's SteamVR_Controller. You have to remember Commands are executed on the Server. Also this line:
    Code (CSharp):
    1. trackedObj = GameObject.FindWithTag("Controller").GetComponent<SteamVR_TrackedObject>();
    I believe is eventually going to cause you problems. If this "Controller" is a child of the player, then at some point you're going to have lots of "Controller"s in the scene. Lastly, the only other thing I can see that it might be, is does your joint.gameObject have a NetworkTransform component on it? If not, that would explain why the server sees it being thrown, but nobody else knows its transform (or rigidbody in this case) is being altered.
     
  8. emulo2

    emulo2

    Joined:
    Apr 15, 2015
    Posts:
    21
    Thank you! I tried everything but it does not work :(. As a host the objects spawns on the controller. As a child, the object spawns in the middle of my player. So I think he can’t find the right position. The attached objects are correct in the inspector if I join as a client.


    Maybe the server can not use the position in the Childs of the player? The important part is this:
    Code (CSharp):
    1.     [Command]
    2.     public void Cmd_Shoot()
    3.     {
    4.         if (joint == null) {
    5.             var go = Instantiate (prefab);    
    6.             NetworkServer.Spawn (go);
    7.             go.transform.position = attachPoint.transform.position;  //  attachPoint = Point on the Child of the player (public Rigidbody attachPoint)
    8.  
    9.             joint = go.AddComponent<FixedJoint> ();
    10.             joint.connectedBody = attachPoint;
    11.         }
    12.     }


    If this will work, the problem maybe is solved. At the moment it spawns the prefab but the joint function will not work. The joint function refers to the child of the player (and its correct in the inspector if I join as a client).. :(. If i play as a host, the joint function works. Any ideas?
     
  9. Wtyson

    Wtyson

    Joined:
    Aug 15, 2014
    Posts:
    57
    where are you actually calling these functions from the root of the object or from some where else
     
  10. emulo2

    emulo2

    Joined:
    Apr 15, 2015
    Posts:
    21
    I call these function from the parent / the spawned player


    This is the point where i try to connect the objects (FixedJoint)

     
    Last edited: Dec 12, 2015
  11. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    1. What exactly is the "prefab" that you're Instantiating?

    2. Try this:
    Code (CSharp):
    1. [Command]
    2. public void Cmd_Shoot()
    3. {
    4.     if (joint == null) {
    5.         var go = Instantiate (prefab);  
    6.         joint = go.AddComponent<FixedJoint> ();
    7.         joint.connectedBody = attachPoint;
    8.         go.transform.position = attachPoint.transform.position;
    9.         NetworkServer.Spawn (go);
    10.     }
    11. }
     
  12. emulo2

    emulo2

    Joined:
    Apr 15, 2015
    Posts:
    21
    I try to spawn this object:




    If i use your code, the object spawns and disappears directly (and pops up on a different place) :(. No matter if I am the host or the client.
     
  13. emulo2

    emulo2

    Joined:
    Apr 15, 2015
    Posts:
    21
    So i added a NetworkTransform to the attached point of the ball. It helps a little bit. Now the ball spawns on the right place. But:

    The ball only gets visible and immediately disappears on the server, if the client attached the ball to the controller. So if the client move the server spawned object, the object disappears for the host. Why?


    If the object spawns on the ground, the client can see the object rolling over the floor.

    Any Ideas?
     
  14. MrTieGuy

    MrTieGuy

    Joined:
    Mar 9, 2017
    Posts:
    1
    Do you have a snippet of code that Destroys if a value hits 0?