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.

Bug Removing NetworkBehaviour in runtime

Discussion in 'Multiplayer' started by newFort4, Oct 10, 2022.

  1. newFort4

    newFort4

    Joined:
    Sep 10, 2016
    Posts:
    2
    Hello. I have PlayerMovement.cs script
    Code (CSharp):
    1. using Unity.Netcode;
    2. using UnityEngine;
    3.  
    4. [RequireComponent(typeof(CharacterController))]
    5. public class PlayerMovement : NetworkBehaviour
    6. {
    7.     [SerializeField]
    8.     private GameObject cameraObject;
    9.  
    10.     [SerializeField]
    11.     private float speed = 4f;
    12.     [SerializeField]
    13.     private float accelerationMultiplier = 1.7f;
    14.  
    15.     [SerializeField]
    16.     private float rotationSensitivity = 4f;
    17.  
    18.     private const float GravityAcceleration = -9.81f;
    19.     private const float UpAngleCameraLimit = -0.7f;
    20.     private const float DownAngleCameraLimit = 0.7f;
    21.  
    22.     private const float DeltaAngle = 0.0001f;
    23.  
    24.     private CharacterController characterController;
    25.  
    26.     public override void OnNetworkSpawn()
    27.     {
    28.         if (IsOwner)
    29.         {
    30.             characterController = GetComponent<CharacterController>();
    31.  
    32.             cameraObject.SetActive(true);
    33.  
    34.             if (Camera.main)
    35.             {
    36.                 Destroy(Camera.main.gameObject);
    37.             }
    38.         } else
    39.         {
    40.             enabled = false;
    41.         }
    42.     }
    43.  
    44.     private void Update()
    45.     {
    46.         Move();
    47.         LookCamera();
    48.     }
    49.  
    50.     private void Move()
    51.     {
    52.         var horizontal = Input.GetAxis("Horizontal");
    53.         var vertical = Input.GetAxis("Vertical");
    54.  
    55.         var acceleration = Input.GetAxis("Acceleration") > 0.01f ? accelerationMultiplier : 1;
    56.  
    57.         var gravityMovement = characterController.isGrounded ? 0 : GravityAcceleration;
    58.  
    59.         // ToDo: (Potential optimization) Don't create new Vector3 each time. Create private variable in class and use it as cache.
    60.         _ = characterController.Move(acceleration * speed * Time.deltaTime * transform.TransformDirection(new Vector3(horizontal, 0, vertical)));
    61.         _ = characterController.Move(Time.deltaTime * new Vector3(0, gravityMovement));
    62.     }
    63.  
    64.     private void LookCamera()
    65.     {
    66.         var horizontal = Input.GetAxis("Mouse X");
    67.         var vertical = Input.GetAxis("Mouse Y");
    68.  
    69.         if (UpAngleCameraLimit < cameraObject.transform.localRotation.x && cameraObject.transform.localRotation.x < DownAngleCameraLimit)
    70.         {
    71.             cameraObject.transform.Rotate(-vertical * rotationSensitivity, 0, 0);
    72.         }
    73.         else
    74.         {
    75.             var limitedQuaternion = cameraObject.transform.localRotation;
    76.  
    77.             if (cameraObject.transform.localRotation.x < UpAngleCameraLimit)
    78.             {
    79.                 limitedQuaternion.x = UpAngleCameraLimit + DeltaAngle;
    80.             }
    81.  
    82.             if (cameraObject.transform.localRotation.x > DownAngleCameraLimit)
    83.             {
    84.                 limitedQuaternion.x = DownAngleCameraLimit - DeltaAngle;
    85.             }
    86.  
    87.             cameraObject.transform.localRotation = limitedQuaternion;
    88.         }
    89.  
    90.         transform.Rotate(horizontal * rotationSensitivity * Vector3.up);
    91.     }
    92. }
    93.  
    When I try remove this script (Destroy(this)) on not ownable player prefabs, NetworkManager no longer synchronises Transform (last position before removing the script), but when Disabling the script everything works fine.

    Is that expected behaviour? If yes please send link to documentation with this issue described.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    1,877
    You shouldn't destroy NetworkBehaviour (or NetworkObject). Disabling is fine. And for entire objects you'd use Spawn/Despawn.

    I setup my player to have Local and Remote child objects underneath it, which get enabled/disabled depending on whether the player is controlled locally or a remote player. That way I can also show name tags above remote players, without the local player seeing his own name when looking up.
     
    newFort4 likes this.