Search Unity

Two UNet questions - position and laggy

Discussion in 'UNet' started by RedDragonka, Feb 8, 2019.

  1. RedDragonka

    RedDragonka

    Joined:
    May 11, 2018
    Posts:
    1
    Hello, I have two questions.
    I want make a game where player can take gameObject and transport them.

    1. Use i UNet right?
    Player send to server with [Command] and server send to other Player with [RpcClient].
    The difficult is the GameObject that the player transport. I'm not sure if I did it right.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class PlayerControl : NetworkBehaviour
    7. {
    8.     public GameObject playerCamera;
    9.     public int movePower;
    10.     public int maxMovePower;
    11.     public int jumpPower;
    12.     public float distanceJumpBlock;
    13.     public float gravityPower;
    14.  
    15.     public GameObject handObject;
    16.  
    17.     Rigidbody rigidbody;
    18.     CapsuleCollider collider;
    19.  
    20.     GameObject materialDetector;
    21.  
    22.     void Start()
    23.     {
    24.         rigidbody = GetComponent<Rigidbody>();
    25.         collider = GetComponent<CapsuleCollider>();
    26.         playerCamera = Camera.main.gameObject;
    27.      
    28.         if (!isLocalPlayer )
    29.               GetComponent<PlayerControl>().enabled = false;
    30.      
    31.     }
    32.  
    33.     void Update()
    34.     {  
    35.         KeyInput();
    36.         Gravity();
    37.         HandObject();    
    38.     }
    39.    
    40.    
    41.  
    42.     void Gravity()
    43.     {
    44.         if(!IsGrounded())
    45.             rigidbody.velocity = rigidbody.velocity - playerCamera.transform.GetChild(0).up * (Time.deltaTime * gravityPower);
    46.        
    47.     }
    48.  
    49.     bool IsGrounded()
    50.     {
    51.         return Physics.Raycast(collider.bounds.center, Vector3.down, distanceJumpBlock);
    52.     }
    53.  
    54.  
    55.     void KeyInput()
    56.     {
    57.    
    58.      
    59.         if (rigidbody.velocity.magnitude > maxMovePower)
    60.         {
    61.             rigidbody.velocity = Vector3.ClampMagnitude(rigidbody.velocity, maxMovePower);
    62.         }
    63.  
    64.         if (Input.GetKey(KeyCode.W))
    65.         {
    66.             rigidbody.velocity = rigidbody.velocity + playerCamera.transform.GetChild(0).forward * Time.deltaTime * movePower;
    67.             Vector3 pos = Quaternion.LookRotation(rigidbody.velocity).eulerAngles;        
    68.             transform.rotation = Quaternion.Euler(0, pos.y, 0);
    69.         }
    70.  
    71.         if (Input.GetKey(KeyCode.S))
    72.         {
    73.             rigidbody.velocity = rigidbody.velocity - playerCamera.transform.GetChild(0).forward * Time.deltaTime * movePower;
    74.             Vector3 pos = Quaternion.LookRotation(rigidbody.velocity).eulerAngles;
    75.             transform.rotation = Quaternion.Euler(0, pos.y, 0);
    76.         }
    77.  
    78.         if (Input.GetKey(KeyCode.D))
    79.         {
    80.             rigidbody.velocity = rigidbody.velocity + playerCamera.transform.GetChild(0).right * Time.deltaTime * movePower;
    81.             Vector3 pos = Quaternion.LookRotation(rigidbody.velocity).eulerAngles;
    82.             transform.rotation = Quaternion.Euler(0, pos.y, 0);
    83.         }
    84.  
    85.         if (Input.GetKey(KeyCode.A))
    86.         {
    87.  
    88.             rigidbody.velocity = rigidbody.velocity - playerCamera.transform.GetChild(0).right * Time.deltaTime * movePower;
    89.             Vector3 pos = Quaternion.LookRotation(rigidbody.velocity).eulerAngles;
    90.             transform.rotation = Quaternion.Euler(0, pos.y, 0);
    91.         }
    92.      
    93.         if (Input.GetKeyDown(KeyCode.Space) && IsGrounded())
    94.         {        
    95.             rigidbody.velocity = rigidbody.velocity + playerCamera.transform.GetChild(0).up * ( jumpPower);    
    96.         }
    97.  
    98.         if (Input.GetKeyDown(KeyCode.E))
    99.         {
    100.             if (handObject == null)
    101.             {
    102.                 if (materialDetector != null)
    103.                     CmdPickUpHandObject(materialDetector);
    104.             }
    105.             else
    106.             {
    107.                 CmdDropHandObject();
    108.             }
    109.         }
    110.     }
    111.  
    112.  
    113.     void HandObject()
    114.     {
    115.         CmdSetHandObjectPosition();
    116.     }
    117.  
    118.     [Command]
    119.     void CmdSetHandObjectPosition()
    120.     {
    121.         if (handObject != null)
    122.         {
    123.              handObject.GetComponent<Material>().CmdSetPosition(transform.position + transform.forward);
    124.           //   handObject.GetComponent<Material>().SetPosition(transform.position + transform.forward);
    125.         }
    126.  
    127.     }
    128.  
    129.     [Command]
    130.     void CmdPickUpHandObject(GameObject handObject)
    131.     {
    132.         RpcPickUpHandObject(handObject);
    133.     }
    134.  
    135.     [ClientRpc]
    136.     void RpcPickUpHandObject(GameObject handObject)
    137.     {
    138.         handObject.GetComponent<Rigidbody>().useGravity = false;
    139.         handObject.GetComponent<BoxCollider>().enabled = false;
    140.        
    141.         this.handObject = handObject;
    142.         materialDetector = null;
    143.     }
    144.  
    145.     [Command]
    146.     void CmdDropHandObject()
    147.     {
    148.         RpcDropHandObject();
    149.     }
    150.  
    151.     [ClientRpc]
    152.     void RpcDropHandObject()
    153.     {
    154.         handObject.GetComponent<Rigidbody>().useGravity = true;
    155.         handObject.GetComponent<BoxCollider>().enabled = true;
    156.      
    157.         handObject = null;
    158.     }
    159.  
    160.  
    161.     private void OnTriggerStay(Collider other)
    162.     {
    163.         if (other.gameObject.tag == "Material")
    164.         {
    165.             materialDetector = other.gameObject;
    166.         }
    167.      
    168.          
    169.     }
    170.  
    171.     private void OnTriggerExit(Collider other)
    172.     {
    173.         if (other.gameObject.tag == "Material")
    174.         {
    175.             materialDetector = null;
    176.         }
    177.      
    178.     }
    179.  
    180. }
    181.  

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5. public class Material: NetworkBehaviour
    6. {
    7.     [SyncVar(hook = "MoveTo")]
    8.     public Vector3 pos;
    9.  
    10.     void MoveTo(Vector3 newPosition)
    11.     {
    12.         transform.position = newPosition;
    13.         transform.rotation = new Quaternion(0, 0, 0, 0);
    14.     }
    15.  
    16.     [Command]
    17.     public void CmdSetPosition(Vector3 pos)
    18.     {
    19.    
    20.         RpcSetPosition(pos);
    21.     }
    22.  
    23.     [ClientRpc]
    24.     public void RpcSetPosition(Vector3 pos)
    25.     {
    26.         this.pos = pos;
    27.    
    28.     }
    29.  
    30.     public void SetPosition(Vector3 pos)
    31.     {
    32.    
    33.         transform.position = pos;
    34.         CmdSetPosition(pos);
    35.     }
    36. }
    37.  

    2. Why is it so laggy? (Left is server and right is player)

     
  2. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    UNET's NetworkTransform has no interpolation. We have been requesting this since day one, but it never happened.
    There are a few options.
    • You could find the community made NetworkTransform script that uses interpolation. It was posted on this forum a few years ago, google might help.
    • You could add interpolation to NetworkTransform.
    • You could use Mirror, our community fork of UNET. We have interpolation.
     
  3. thegreatzebadiah

    thegreatzebadiah

    Joined:
    Nov 22, 2012
    Posts:
    836
    Personally I recommend SmoothSync, it works with UNet, Mirror, or Photon and it's got a ton of settings to get syncing just right.