Search Unity

UNET need some guidance with basics

Discussion in 'UNet' started by BrendanKZN, Jun 13, 2016.

  1. BrendanKZN

    BrendanKZN

    Joined:
    Jun 22, 2011
    Posts:
    157
    Hi Guys, thanks for taking a look.

    I am rather new to UNET and hoping to learn. Not the most experienced with C# either.
    Having troubles visualizing and understanding who owns what and who must run what. So here is my workings in pseudo code.

    Server starts, spawns a vehicle which was pre placed in the scene.
    Player Object (Actual player being capsule for now) spawns for the client when he joins.
    Player walks up to vehicle and presses E (Raycast) to enter the "Vehicle" and runs the following function in a script "PlayerInteractions" which is attached to the player.

    EnterVehicle();
    {
    Disable player collider
    Disable player controlls if is local player
    Disable player gravity
    Enable player iskinematic
    Move player to centre of vehicle (or seat later)
    Parent player to vehicle
    Enable vehicle controls for local player
    }

    Should I be running this on the server with Command? Do I need to use RpcClient? Will the changes to the player components sync to the other clients? I have tried various combinations and having mixed up results.
    Am I approaching this completely wrong? This all works well in a single player environment.

    Some pseudo code on the correct procedure / route would be immensely appreciated.

    Thank you for your time.
     
  2. BrendanKZN

    BrendanKZN

    Joined:
    Jun 22, 2011
    Posts:
    157
    Hi Guys, just an update. It looks like I got most of it working somewhat. I have pasted the code below if anyone with more experience can tell me if I am doing this horribly wrong or am I on the right track? I can probably shorten this a bit without repeating code but this is rough coding for testing and learning.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class PlayerInteractions : NetworkBehaviour
    6. {
    7.     [SerializeField]
    8.     private Camera cam; //Camera for interactive Raycast
    9.  
    10.     private Transform _interactiveRayHitTarget; //Transform of object that was hit with the interactive Raycast check.
    11.  
    12.     public GameObject vehicle01; //List of vehicles that can spawn, maybe move this to another script later.
    13.  
    14.     public string playerState = "OutsideVehicle";
    15.  
    16.     void Update()
    17.     {
    18.         if (Input.GetKeyDown(KeyCode.E))
    19.         {
    20.             FireInteractiveRay();
    21.         }
    22.     }
    23.  
    24.     void FireInteractiveRay()
    25.     {
    26.         Debug.DrawRay(cam.transform.position, cam.transform.forward * 3, Color.green, 0.5f);
    27.  
    28.         RaycastHit _hit;
    29.         if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, 3))
    30.         {
    31.             //Debug.Log(_hit.collider.tag);
    32.             //if (_hit.collider.name == "CarSpawnPanel")
    33.             //{
    34.             //_interactiveRayHitTarget = _hit.transform.FindChild("SpawnTarget");
    35.             //CmdSpawnVehicle();
    36.             //}
    37.  
    38.             if (_hit.collider.tag == "Vehicle" && playerState == "OutsideVehicle")
    39.             {
    40.                 _interactiveRayHitTarget = _hit.transform;//Testing
    41.                 CmdEnterVehicle(_hit.collider.gameObject);//Testing
    42.             }
    43.         }
    44.  
    45.         else if (playerState == "InsideVehicle")
    46.         {
    47.             CmdExitVehicle(_interactiveRayHitTarget.gameObject);//Last ray hit the vehicle so it must still be correct.
    48.         }
    49.     }
    50.  
    51.     [Command]
    52.     void CmdEnterVehicle(GameObject _targetVehicle)
    53.     {
    54.         _targetVehicle.GetComponent<NetworkIdentity>().AssignClientAuthority(connectionToClient); //Give authority over the vehicle so we can move it.
    55.         RpcEnterVehicle(_targetVehicle);
    56.     }
    57.  
    58.     [ClientRpc]
    59.     void RpcEnterVehicle(GameObject _target)//Disable player controls, colliders etc.
    60.     {
    61.         GetComponent<CapsuleCollider>().enabled = false;
    62.         GetComponent<Rigidbody>().useGravity = false;
    63.         GetComponent<Rigidbody>().isKinematic = true;
    64.  
    65.         if (isLocalPlayer) //Toggle state of player controlls and motor on local pc only.
    66.         {
    67.             GetComponent<PlayerController>().enabled = false;
    68.             GetComponent<PlayerMotor>().enabled = false;
    69.             _target.GetComponent<Vehicle_Controls>().enabled = true;
    70.             playerState = "InsideVehicle";//Set state
    71.         }
    72.  
    73.         transform.parent = _target.transform;//Child player to vehicle
    74.         transform.position = _target.transform.position;//Move player inside vehicle
    75.         transform.rotation = _target.transform.rotation;//Rotate player to face forward in vehicle
    76.         GetComponent<NetworkTransform>().enabled = false;
    77.         GetComponent<NetworkTransformChild>().enabled = false;
    78.  
    79.     }
    80.  
    81.     [Command]
    82.     void CmdExitVehicle(GameObject _targetVehicle)
    83.     {
    84.         _targetVehicle.GetComponent<NetworkIdentity>().RemoveClientAuthority(connectionToClient); //Remove authority over the vehicle
    85.         RpcExitVehicle(_targetVehicle);
    86.     }
    87.  
    88.     [ClientRpc]
    89.     void RpcExitVehicle(GameObject _target)//Enable player controls, colliders etc.
    90.     {
    91.         transform.position = transform.parent.TransformPoint(Vector3.left * 2);//Move player outside of vehicle
    92.         transform.parent = null;//Unchild player from vehicle
    93.  
    94.         GetComponent<CapsuleCollider>().enabled = true;
    95.         GetComponent<Rigidbody>().useGravity = true;
    96.         GetComponent<Rigidbody>().isKinematic = false;
    97.  
    98.         if (isLocalPlayer) //Toggle state of player controlls and motor on local pc only.
    99.         {
    100.             GetComponent<PlayerController>().enabled = true;
    101.             GetComponent<PlayerMotor>().enabled = true;
    102.             _target.GetComponent<Vehicle_Controls>().enabled = false;
    103.             playerState = "OutsideVehicle";//Set state
    104.         }
    105.  
    106.         GetComponent<NetworkTransform>().enabled = true;
    107.         GetComponent<NetworkTransformChild>().enabled = true;
    108.     }
    109.  
    110.     [Command]
    111.     void CmdSpawnVehicle()
    112.     {
    113.         GameObject vehicle = (GameObject)Instantiate(vehicle01, _interactiveRayHitTarget.position, _interactiveRayHitTarget.rotation);
    114.         NetworkServer.Spawn(vehicle);
    115.     }
    116. }
    117.  
     
  3. Deleted User

    Deleted User

    Guest

    did u Find that RemoveAuthority, cause server lag and lag Unity Editor untill player disconnect?
     
  4. BrendanKZN

    BrendanKZN

    Joined:
    Jun 22, 2011
    Posts:
    157
    Hi @Wobes . Unfortunately I have not gotten to that stage to notice any lag or complications with RemoveClientAuthority yet. I might take a closer look and keep that in mind for you.
     
  5. Deleted User

    Deleted User

    Guest

    thank you so much abot that.