Search Unity

Client actions not occurring on a non-player object UNET

Discussion in 'UNet' started by jimmyman123, Mar 10, 2017.

  1. jimmyman123

    jimmyman123

    Joined:
    Feb 2, 2017
    Posts:
    1
    So I got my game to work where the players are able to spawn in. However, when spawning objects into the scene I got it where the host's actions are synced to both the server and clients, but the client can't take any actions relating to the non-player object ball, the ball would just remain unchanged on both the client and server. I've searched many forums, but couldn't seem to figure out what the issue is. I've toggled many network transform options, but I don't think it helped. Thanks

    Unity 5.5

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. public class BallPlayer : NetworkBehaviour
    5. {
    6.     public GameObject pref;
    7.     const int nudgeAmount = 2;
    8.     bool isGrounded = true;
    9.     Ray ray;
    10.     RaycastHit hit;
    11.     [Range(0f, 10f)]
    12.     public int ballAmount;
    13.     public GameObject ballPosition;
    14.     [ClientCallback]
    15.     void OnCollisionEnter(Collision col)
    16.     {
    17.         if (!isLocalPlayer)
    18.             return;
    19.         //Jump test
    20.         if (col.gameObject.name.Contains("floor"))
    21.         {
    22.             isGrounded = true;
    23.         }
    24.     }
    25.  
    26.     float timeLeft = 5f;
    27.     bool timerDone = false;
    28.  
    29.     void Update()
    30.     {
    31.         if (isServer == true)
    32.         {
    33.             timeLeft -= Time.deltaTime;
    34.             //Spawn Balls After 5 seconds
    35.             if ((timeLeft < 0) && (timerDone == false))
    36.             {
    37.                 timerDone = true;
    38.                 cmdSpawn();
    39.             }
    40.  
    41.         }
    42.  
    43.         if (!isLocalPlayer)
    44.             return;
    45.         Cmdclicking();
    46.         //Moves with mouse
    47.         GetComponent<Rigidbody>().transform.Rotate(new Vector3(0, Input.GetAxis("Mouse X"), 0) * Time.deltaTime * 800);
    48.         cmdmove();
    49.  
    50.     }
    51.  
    52.     [Command]
    53.     public void Cmdclicking()
    54.     {
    55.         if (Input.GetMouseButtonDown(0))
    56.         {
    57.             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    58.             if (Physics.Raycast(ray, out hit))
    59.             {
    60.                 if (hit.collider.name.Contains("ball"))
    61.                 {
    62.                     hit.collider.GetComponent<NetworkTransform>().rigidbody3D.velocity = (transform.forward * nudgeAmount * 10);
    63.  
    64.                 }
    65.             }
    66.  
    67.         }
    68.  
    69.  
    70.     }
    71.  
    72.     public void cmdmove()
    73.  
    74.     {
    75.         //Player movements
    76.         if ((Input.GetKey(KeyCode.Space)) && (isGrounded))
    77.         {
    78.             GetComponent<Rigidbody>().AddForce((transform.up * nudgeAmount * 200));
    79.  
    80.             isGrounded = false;
    81.         }
    82.  
    83.         if (Input.GetKey(KeyCode.A))
    84.         {
    85.  
    86.             GetComponent<Rigidbody>().AddForce(-(transform.right * nudgeAmount));
    87.  
    88.         }
    89.  
    90.         if (Input.GetKey(KeyCode.D))
    91.         {
    92.  
    93.             GetComponent<Rigidbody>().AddForce(transform.right * nudgeAmount);
    94.  
    95.         }
    96.  
    97.  
    98.  
    99.  
    100.         if (Input.GetKey(KeyCode.W))
    101.         {
    102.             GetComponent<Rigidbody>().AddForce(transform.forward * nudgeAmount);
    103.  
    104.         }
    105.  
    106.         if (Input.GetKey(KeyCode.S))
    107.         {
    108.             GetComponent<Rigidbody>().AddForce(-(transform.forward * nudgeAmount));
    109.         }
    110.  
    111.     }
    112.  
    113.  
    114.     public void cmdSpawn()
    115.     {
    116.         //Spawning balls
    117.         for (int i = 0; i < ballAmount; i++)
    118.         {
    119.             GameObject obj = Instantiate(pref, GameObject.Find("BallPos").transform.position + new Vector3(i, 0, 0), Quaternion.identity);
    120.             NetworkServer.SpawnWithClientAuthority(obj, NetworkServer.connections[0]);
    121.         }
    122.     }
    123. }
    124.  
     
  2. Karsten

    Karsten

    Joined:
    Apr 8, 2012
    Posts:
    187
    Hi,

    1. Cmdclicking is a [Command] this is called only, and only on the server wich you do in line 45
    2. There for line 55 evaluates only true if the mouse is clicked on the server (!!!!!)

    There are other potential issues i see but to tell more i would need to see the whole setup, for example do you use a rigidbody sync component or not ect ect