Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Client doesn't move at all

Discussion in 'Netcode for GameObjects' started by AngelHYYPE09, Nov 20, 2023.

  1. AngelHYYPE09

    AngelHYYPE09

    Joined:
    Jan 31, 2022
    Posts:
    15
    I have a player prefab that contains a character controller, rigidbody, and it uses ClientNetworkTransform. It has it's player script which is very simple, in the update function, the movement function gets called which is a ClientRpc.
    When the game starts and the host enters, everything works fine, the host can move with no issues and the colliders work great as well, but when the client enters, he appears either floating or stuck in the floor, and he can't move or rotate the camera.

    This is my Player Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using Unity.Netcode;
    6.  
    7. [RequireComponent(typeof(CharacterController))]
    8. public class PlayerMovementSOLO : NetworkBehaviour
    9. {
    10.     // my variables
    11.  
    12.     public Camera playerCamera;
    13.     public Transform playertrans;
    14.     public float walkSpeed = 6f;
    15.     public float runSpeed = 12f;
    16.     public float jumpPower = 7f;
    17.     public float gravity = 10f;
    18.  
    19.     // Pick Objects - System
    20.     [SerializeField] private LayerMask PickMask;
    21.     [SerializeField] private LayerMask PickMask_Pepperoni;
    22.     [SerializeField] private LayerMask PickMask_Mushroom;
    23.     [SerializeField] private LayerMask PickMask_Olive;
    24.     [SerializeField] private LayerMask PickMask_Pineapple;
    25.     [SerializeField] private LayerMask PickMask_Dough;
    26.     [SerializeField] private Transform TargetPick;
    27.     [SerializeField] private GameObject TargetCross;
    28.     [Space]
    29.     [SerializeField] private float PickRange;
    30.     private Rigidbody CurrentObj;
    31.     [Space]
    32.     // Pick Objects - Prefabs
    33.     public Rigidbody Pepobj;
    34.     public Rigidbody Mushobj;
    35.     public Rigidbody Oliveobj;
    36.     public Rigidbody Pineobj;
    37.     public Rigidbody Doughobj;
    38.     [Space]
    39.     public float lookSpeed = 2f;
    40.     public float lookXLimit = 45f;
    41.     [Space]
    42.     // Pick Objects - Machines
    43.     public GameObject TargetSpawnPp;
    44.     public GameObject TargetSpawnMush;
    45.     public GameObject TargetSpawnOlive;
    46.     public GameObject TargetSpawnPine;
    47.     public GameObject TargetSpawnDough;
    48.  
    49.     Vector3 moveDirection = Vector3.zero;
    50.     float rotationX = 0;
    51.  
    52.     public bool canMove = true;
    53.     public bool isSauceTomato;
    54.  
    55.     // Esentials
    56.     public static PlayerMovementSOLO inst;
    57.     public int IngCount;
    58.     public bool IsTookSauceTomato;
    59.     private CameraNetHold camhold;
    60.  
    61.  
    62.     public CharacterController characterController;
    63.  
    64.  
    65.     void Awake()
    66.     {
    67.         inst = this;
    68.     }
    69.  
    70.     // Start is called before the first frame update
    71.     void Start()
    72.     {
    73.         transform.position = new Vector3(Random.Range(3, 5), -1.5f, Random.Range(8, 11));
    74.         characterController = GetComponent<CharacterController>();
    75.         Cursor.lockState = CursorLockMode.Locked;
    76.         Cursor.visible = false;
    77.         // here i search all the necesarry components for my game
    78.         TargetCross = GameObject.Find("crosshair_img");
    79.         TargetSpawnPp = GameObject.Find("spawnpos_peperr");
    80.         TargetSpawnMush = GameObject.Find("spawnpos_mushrr");
    81.         TargetSpawnOlive = GameObject.Find("spawnpos_oliverr");
    82.         TargetSpawnPine = GameObject.Find("spawnpos_pineapplerr");
    83.         TargetSpawnDough = GameObject.Find("spawndoughtarget");
    84.  
    85.     }
    86.  
    87.     // Update is called once per frame
    88.     private void Update()
    89.     {
    90.         //Here i call the function for the movement
    91.         if (!IsOwner)
    92.         {
    93.             return;
    94.         }
    95.  
    96.         movementinpOnClientRpc();
    97.     }
    98.  
    99.     // Fixed
    100.     void FixedUpdate()
    101.     {
    102.         if (!IsOwner)
    103.         {
    104.             return;
    105.         }
    106.  
    107.         // PICKUP VELOCITY
    108.         if (CurrentObj)
    109.         {
    110.             Vector3 DirectionToPoint = TargetPick.position - CurrentObj.position;
    111.             float DistanceToPoint = DirectionToPoint.magnitude;
    112.  
    113.             CurrentObj.velocity = DirectionToPoint * 12f * DistanceToPoint;
    114.  
    115.         }
    116.    
    117.     }
    118.  
    119.     // My movement script
    120.     // As you can see, it's a ClientRpc (I also tried with ServerRpc and NetworkTransform)
    121.  
    122.     [ClientRpc]
    123.     private void movementinpOnClientRpc()
    124.     {
    125.  
    126.         // Press Left Shift to run
    127.         Vector3 forward = transform.TransformDirection(Vector3.forward);
    128.         Vector3 right = transform.TransformDirection(Vector3.right);
    129.  
    130.         bool isRunning = Input.GetKey(KeyCode.LeftShift);
    131.         float curSpeedX = canMove ? (isRunning ? runSpeed : walkSpeed) * Input.GetAxis("Vertical") : 0;
    132.         float curSpeedY = canMove ? (isRunning ? runSpeed : walkSpeed) * Input.GetAxis("Horizontal") : 0;
    133.         float movementDirectionY = moveDirection.y;
    134.         moveDirection = (forward * curSpeedX) + (right * curSpeedY);
    135.  
    136.         if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
    137.         {
    138.             moveDirection.y = jumpPower;
    139.         }
    140.         else
    141.         {
    142.             moveDirection.y = movementDirectionY;
    143.         }
    144.  
    145.         if (!characterController.isGrounded)
    146.         {
    147.             moveDirection.y -= gravity * Time.deltaTime;
    148.         }
    149.  
    150.         #region Rotation Handles
    151.         characterController.Move(moveDirection * Time.deltaTime);
    152.  
    153.         if (canMove)
    154.         {
    155.             rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
    156.             rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
    157.             playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
    158.             transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
    159.         }
    160.  
    161.         #endregion
    162.  
    163.         //Starting here, it's code for other things like grabbing stuff or manipulating stuff. Nothing relevant to the important for the movement.
    164.  
    165.         #region Pick System - Update
    166.         if (Input.GetMouseButtonDown(0))
    167.         {
    168.             // CHECK
    169.             if (CurrentObj)
    170.             {
    171.                 CurrentObj.useGravity = true;
    172.                 CurrentObj = null;
    173.                 isSauceTomato = false;
    174.                 return;
    175.             }
    176.  
    177.             // LAUNCH RAYCAST
    178.             Ray CamRay = playerCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    179.             if (Physics.Raycast(CamRay, out RaycastHit HitInfo, PickRange, PickMask))
    180.             {
    181.                 CurrentObj = HitInfo.rigidbody;
    182.                 CurrentObj.useGravity = false;
    183.  
    184.             }
    185.  
    186.             if (Physics.Raycast(CamRay, out RaycastHit HitInfo_pepperoni, PickRange, PickMask_Pepperoni))
    187.             {
    188.                 Pepperonimachinescript.inst.OnPressedPepperoni();
    189.                 Instantiate(Pepobj, TargetSpawnPp.transform.position, Pepobj.transform.rotation);
    190.             }
    191.  
    192.             if (Physics.Raycast(CamRay, out RaycastHit HitInfo_mushroom, PickRange, PickMask_Mushroom))
    193.             {
    194.                 Mushroommachinescript.inst.OnPressedMushroom();
    195.                 Instantiate(Mushobj, TargetSpawnMush.transform.position, Mushobj.transform.rotation);
    196.             }
    197.  
    198.             if (Physics.Raycast(CamRay, out RaycastHit HitInfo_blackolive, PickRange, PickMask_Olive))
    199.             {
    200.                 Bolivemachinescript.inst.OnPressedOlive();
    201.                 Instantiate(Oliveobj, TargetSpawnOlive.transform.position, Oliveobj.transform.rotation);
    202.             }
    203.  
    204.             if (Physics.Raycast(CamRay, out RaycastHit HitInfo_pineapple, PickRange, PickMask_Pineapple))
    205.             {
    206.                 Pineapplemachinescript.inst.OnPressedPineapple();
    207.                 Instantiate(Pineobj, TargetSpawnPine.transform.position, Pineobj.transform.rotation);
    208.             }
    209.  
    210.             if (Physics.Raycast(CamRay, out RaycastHit HitInfo_doughdispense, PickRange, PickMask_Dough))
    211.             {
    212.                 DoughDispense.inst.OnPressedDough();
    213.                 Instantiate(Doughobj, TargetSpawnDough.transform.position, Doughobj.transform.rotation);
    214.             }
    215.  
    216.  
    217.  
    218.  
    219.  
    220.             if (Physics.Raycast(CamRay, out RaycastHit HitInfo_Sauce, PickRange, PickMask))
    221.             {
    222.                 if (HitInfo.transform.tag == "TomatoSauce" || HitInfo.transform.tag == "HotSauce" || HitInfo.transform.tag == "SusSauce")
    223.                 {
    224.                     CurrentObj = HitInfo_Sauce.rigidbody;
    225.                     CurrentObj.useGravity = false;
    226.                     isSauceTomato = true;
    227.                 }
    228.             }
    229.  
    230.             if (isSauceTomato)
    231.             {
    232.                 IsTookSauceTomato = true;
    233.                 HitInfo_Sauce.transform.GetComponent<SauceScriptTomato>().CanRot = IsTookSauceTomato;
    234.             }
    235.             else if (!isSauceTomato)
    236.             {
    237.                 IsTookSauceTomato = false;
    238.             }
    239.         }
    240.  
    241.         //This
    242.  
    243.         Ray CamRayDETECT = playerCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    244.         if (Physics.Raycast(CamRayDETECT, out RaycastHit Hitinfo, PickRange, PickMask) && !CurrentObj
    245.         || Physics.Raycast(CamRayDETECT, out RaycastHit Hitinfo_peper, PickRange, PickMask_Pepperoni) && !CurrentObj
    246.         || Physics.Raycast(CamRayDETECT, out RaycastHit Hitinfo_mush, PickRange, PickMask_Mushroom) && !CurrentObj
    247.         || Physics.Raycast(CamRayDETECT, out RaycastHit Hitinfo_bolive, PickRange, PickMask_Olive) && !CurrentObj
    248.         || Physics.Raycast(CamRayDETECT, out RaycastHit Hitinfo_dough, PickRange, PickMask_Dough) && !CurrentObj
    249.         || Physics.Raycast(CamRayDETECT, out RaycastHit Hitinfo_pine, PickRange, PickMask_Pineapple) && !CurrentObj)
    250.         {
    251.             TargetCross.GetComponent<Image>().color = new Color32(43, 255, 0, 255);
    252.         }
    253.         else
    254.         {
    255.             TargetCross.GetComponent<Image>().color = new Color32(0, 0, 0, 255);
    256.         }
    257.  
    258.         if (CurrentObj)
    259.         {
    260.             TargetCross.GetComponent<Image>().color = new Color32(255, 0, 0, 255);
    261.         }
    262.  
    263.         if (isSauceTomato)
    264.         {
    265.             IsTookSauceTomato = true;
    266.             //HitInfo_Saucetom.transform.GetComponent<SauceScriptTomato>().CanRot = true;
    267.             //SauceScriptTomato.inst.CanRot = true;
    268.         }
    269.         else if (!isSauceTomato)
    270.         {
    271.             IsTookSauceTomato = false;
    272.             //HitInfo_Saucetom.transform.GetComponent<SauceScriptTomato>().CanRot = false;
    273.             //SauceScriptTomato.inst.CanRot = false;
    274.         }
    275.  
    276.         #endregion
    277.  
    278.     }
    279.  
    280.  
    281. }
    282.     [ClientRpc]
    283.     private void movementinpOnClientRpc()
    284.     {
    285.  
    286.         // Press Left Shift to run
    287.         Vector3 forward = transform.TransformDirection(Vector3.forward);
    288.         Vector3 right = transform.TransformDirection(Vector3.right);
    289.  
    290.         bool isRunning = Input.GetKey(KeyCode.LeftShift);
    291.         float curSpeedX = canMove ? (isRunning ? runSpeed : walkSpeed) * Input.GetAxis("Vertical") : 0;
    292.         float curSpeedY = canMove ? (isRunning ? runSpeed : walkSpeed) * Input.GetAxis("Horizontal") : 0;
    293.         float movementDirectionY = moveDirection.y;
    294.         moveDirection = (forward * curSpeedX) + (right * curSpeedY);
    295.  
    296.         if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
    297.         {
    298.             moveDirection.y = jumpPower;
    299.         }
    300.         else
    301.         {
    302.             moveDirection.y = movementDirectionY;
    303.         }
    304.  
    305.         if (!characterController.isGrounded)
    306.         {
    307.             moveDirection.y -= gravity * Time.deltaTime;
    308.         }
    309.  
    310.         #region Rotation Handles
    311.         characterController.Move(moveDirection * Time.deltaTime);
    312.  
    313.         if (canMove)
    314.         {
    315.             rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
    316.             rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
    317.             playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
    318.             transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
    319.         }
    320.  
    321.         #endregion
    322.  
    323.         #region Pick System - Update
    324.         if (Input.GetMouseButtonDown(0))
    325.         {
    326.             // CHECK
    327.             if (CurrentObj)
    328.             {
    329.                 CurrentObj.useGravity = true;
    330.                 CurrentObj = null;
    331.                 isSauceTomato = false;
    332.                 return;
    333.             }
    334.  
    335.             // LAUNCH RAYCAST
    336.             Ray CamRay = playerCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    337.             if (Physics.Raycast(CamRay, out RaycastHit HitInfo, PickRange, PickMask))
    338.             {
    339.                 CurrentObj = HitInfo.rigidbody;
    340.                 CurrentObj.useGravity = false;
    341.  
    342.             }
    343.  
    344.             if (Physics.Raycast(CamRay, out RaycastHit HitInfo_pepperoni, PickRange, PickMask_Pepperoni))
    345.             {
    346.                 Pepperonimachinescript.inst.OnPressedPepperoni();
    347.                 Instantiate(Pepobj, TargetSpawnPp.transform.position, Pepobj.transform.rotation);
    348.             }
    349.  
    350.             if (Physics.Raycast(CamRay, out RaycastHit HitInfo_mushroom, PickRange, PickMask_Mushroom))
    351.             {
    352.                 Mushroommachinescript.inst.OnPressedMushroom();
    353.                 Instantiate(Mushobj, TargetSpawnMush.transform.position, Mushobj.transform.rotation);
    354.             }
    355.  
    356.             if (Physics.Raycast(CamRay, out RaycastHit HitInfo_blackolive, PickRange, PickMask_Olive))
    357.             {
    358.                 Bolivemachinescript.inst.OnPressedOlive();
    359.                 Instantiate(Oliveobj, TargetSpawnOlive.transform.position, Oliveobj.transform.rotation);
    360.             }
    361.  
    362.             if (Physics.Raycast(CamRay, out RaycastHit HitInfo_pineapple, PickRange, PickMask_Pineapple))
    363.             {
    364.                 Pineapplemachinescript.inst.OnPressedPineapple();
    365.                 Instantiate(Pineobj, TargetSpawnPine.transform.position, Pineobj.transform.rotation);
    366.             }
    367.  
    368.             if (Physics.Raycast(CamRay, out RaycastHit HitInfo_doughdispense, PickRange, PickMask_Dough))
    369.             {
    370.                 DoughDispense.inst.OnPressedDough();
    371.                 Instantiate(Doughobj, TargetSpawnDough.transform.position, Doughobj.transform.rotation);
    372.             }
    373.  
    374.  
    375.  
    376.  
    377.  
    378.             if (Physics.Raycast(CamRay, out RaycastHit HitInfo_Sauce, PickRange, PickMask))
    379.             {
    380.                 if (HitInfo.transform.tag == "TomatoSauce" || HitInfo.transform.tag == "HotSauce" || HitInfo.transform.tag == "SusSauce")
    381.                 {
    382.                     CurrentObj = HitInfo_Sauce.rigidbody;
    383.                     CurrentObj.useGravity = false;
    384.                     isSauceTomato = true;
    385.                 }
    386.             }
    387.  
    388.             if (isSauceTomato)
    389.             {
    390.                 IsTookSauceTomato = true;
    391.                 HitInfo_Sauce.transform.GetComponent<SauceScriptTomato>().CanRot = IsTookSauceTomato;
    392.             }
    393.             else if (!isSauceTomato)
    394.             {
    395.                 IsTookSauceTomato = false;
    396.             }
    397.         }
    398.  
    399.         Ray CamRayDETECT = playerCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    400.         if (Physics.Raycast(CamRayDETECT, out RaycastHit Hitinfo, PickRange, PickMask) && !CurrentObj
    401.         || Physics.Raycast(CamRayDETECT, out RaycastHit Hitinfo_peper, PickRange, PickMask_Pepperoni) && !CurrentObj
    402.         || Physics.Raycast(CamRayDETECT, out RaycastHit Hitinfo_mush, PickRange, PickMask_Mushroom) && !CurrentObj
    403.         || Physics.Raycast(CamRayDETECT, out RaycastHit Hitinfo_bolive, PickRange, PickMask_Olive) && !CurrentObj
    404.         || Physics.Raycast(CamRayDETECT, out RaycastHit Hitinfo_dough, PickRange, PickMask_Dough) && !CurrentObj
    405.         || Physics.Raycast(CamRayDETECT, out RaycastHit Hitinfo_pine, PickRange, PickMask_Pineapple) && !CurrentObj)
    406.         {
    407.             TargetCross.GetComponent<Image>().color = new Color32(43, 255, 0, 255);
    408.         }
    409.         else
    410.         {
    411.             TargetCross.GetComponent<Image>().color = new Color32(0, 0, 0, 255);
    412.         }
    413.  
    414.         if (CurrentObj)
    415.         {
    416.             TargetCross.GetComponent<Image>().color = new Color32(255, 0, 0, 255);
    417.         }
    418.  
    419.         if (isSauceTomato)
    420.         {
    421.             IsTookSauceTomato = true;
    422.             //HitInfo_Saucetom.transform.GetComponent<SauceScriptTomato>().CanRot = true;
    423.             //SauceScriptTomato.inst.CanRot = true;
    424.         }
    425.         else if (!isSauceTomato)
    426.         {
    427.             IsTookSauceTomato = false;
    428.             //HitInfo_Saucetom.transform.GetComponent<SauceScriptTomato>().CanRot = false;
    429.             //SauceScriptTomato.inst.CanRot = false;
    430.         }
    431.  
    432.         #endregion
    433.  
    434.     }
    435.  
    436.  
    437. }

    Here's a video i uploaded so you can see the problem:


    Any suggestion is happily recieved! Thanks!
     
  2. NoelStephens_Unity

    NoelStephens_Unity

    Unity Technologies

    Joined:
    Feb 12, 2022
    Posts:
    244
    I am assuming it is server authoritative based on the Rpcs... so you should just need to add this to your PlayerMovementSOLO class:
    Code (CSharp):
    1.         public override void OnNetworkSpawn()
    2.         {
    3.             characterController.enabled = IsServer;
    4.         }
    Since the server is the "authority", you just want the non-authoritative instances to "mirror" what is happening on the server side. Otherwise, the non-authoritative instances' (in this case the clients) CharacterController components will fight with the NetworkTransform updates.

    Add the above script snippet and let me know if that resolves your issue?
     
  3. AngelHYYPE09

    AngelHYYPE09

    Joined:
    Jan 31, 2022
    Posts:
    15
    Hey! Thanks for the fast response.
    Unfortunately, it didn't change. I't does deactivate both Character Controllers and i can still move the host, but the client doesn't. I tried activating the character controller of the client in the inspector but it wasn't working either.
    I don't know if you saw the video i sent above, but as you can see, when i try to move the client in the inspector, it goes like crazy, so i'm not sure if it has something to do with the character controllers, maybe it's something in the movement code or the rigidbody's?. I'm new in the Netcode so please be patient with me hahahah
     
  4. NoelStephens_Unity

    NoelStephens_Unity

    Unity Technologies

    Joined:
    Feb 12, 2022
    Posts:
    244
    Ahh... you are using ClientNetworkTransform... which is owner authoritative...
    So,
    I don't think you need a ClientRpc for any of that...
    Try this:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using Unity.Netcode;
    4.  
    5. [RequireComponent(typeof(CharacterController))]
    6. public class PlayerMovementSOLO : NetworkBehaviour
    7. {
    8.     // my variables
    9.  
    10.     public Camera playerCamera;
    11.     public Transform playertrans;
    12.     public float walkSpeed = 6f;
    13.     public float runSpeed = 12f;
    14.     public float jumpPower = 7f;
    15.     public float gravity = 10f;
    16.  
    17.     // Pick Objects - System
    18.     [SerializeField] private LayerMask PickMask;
    19.     [SerializeField] private LayerMask PickMask_Pepperoni;
    20.     [SerializeField] private LayerMask PickMask_Mushroom;
    21.     [SerializeField] private LayerMask PickMask_Olive;
    22.     [SerializeField] private LayerMask PickMask_Pineapple;
    23.     [SerializeField] private LayerMask PickMask_Dough;
    24.     [SerializeField] private Transform TargetPick;
    25.     [SerializeField] private GameObject TargetCross;
    26.     [Space]
    27.     [SerializeField] private float PickRange;
    28.     private Rigidbody CurrentObj;
    29.     [Space]
    30.     // Pick Objects - Prefabs
    31.     public Rigidbody Pepobj;
    32.     public Rigidbody Mushobj;
    33.     public Rigidbody Oliveobj;
    34.     public Rigidbody Pineobj;
    35.     public Rigidbody Doughobj;
    36.     [Space]
    37.     public float lookSpeed = 2f;
    38.     public float lookXLimit = 45f;
    39.     [Space]
    40.     // Pick Objects - Machines
    41.     public GameObject TargetSpawnPp;
    42.     public GameObject TargetSpawnMush;
    43.     public GameObject TargetSpawnOlive;
    44.     public GameObject TargetSpawnPine;
    45.     public GameObject TargetSpawnDough;
    46.  
    47.     Vector3 moveDirection = Vector3.zero;
    48.     float rotationX = 0;
    49.  
    50.     public bool canMove = true;
    51.     public bool isSauceTomato;
    52.  
    53.     // Esentials
    54.     public static PlayerMovementSOLO inst;
    55.     public int IngCount;
    56.     public bool IsTookSauceTomato;
    57.     private CameraNetHold camhold;
    58.  
    59.  
    60.     public CharacterController characterController;
    61.  
    62.  
    63.     void Awake()
    64.     {
    65.         inst = this;
    66.     }
    67.  
    68.     // Start is called before the first frame update
    69.     void Start()
    70.     {
    71.         transform.position = new Vector3(Random.Range(3, 5), -1.5f, Random.Range(8, 11));
    72.         characterController = GetComponent<CharacterController>();
    73.         Cursor.lockState = CursorLockMode.Locked;
    74.         Cursor.visible = false;
    75.         // here i search all the necesarry components for my game
    76.         TargetCross = GameObject.Find("crosshair_img");
    77.         TargetSpawnPp = GameObject.Find("spawnpos_peperr");
    78.         TargetSpawnMush = GameObject.Find("spawnpos_mushrr");
    79.         TargetSpawnOlive = GameObject.Find("spawnpos_oliverr");
    80.         TargetSpawnPine = GameObject.Find("spawnpos_pineapplerr");
    81.         TargetSpawnDough = GameObject.Find("spawndoughtarget");
    82.  
    83.     }
    84.  
    85.     // Fixed
    86.     void FixedUpdate()
    87.     {
    88.         if (!IsOwner)
    89.         {
    90.             return;
    91.         }
    92.  
    93.         // PICKUP VELOCITY
    94.         if (CurrentObj)
    95.         {
    96.             Vector3 DirectionToPoint = TargetPick.position - CurrentObj.position;
    97.             float DistanceToPoint = DirectionToPoint.magnitude;
    98.  
    99.             CurrentObj.velocity = DirectionToPoint * 12f * DistanceToPoint;
    100.  
    101.         }
    102.  
    103.     }
    104.  
    105.     // Update is called once per frame
    106.     private void Update()
    107.     {
    108.         //Here i call the function for the movement
    109.         if (!IsOwner)
    110.         {
    111.             return;
    112.         }
    113.  
    114.         UpdateOwner();
    115.     }
    116.  
    117.     public override void OnNetworkSpawn()
    118.     {
    119.         base.OnNetworkSpawn();
    120.  
    121.         characterController.enabled = IsOwner;
    122.     }
    123.  
    124.     private void UpdateOwner()
    125.     {
    126.  
    127.         // Press Left Shift to run
    128.         Vector3 forward = transform.TransformDirection(Vector3.forward);
    129.         Vector3 right = transform.TransformDirection(Vector3.right);
    130.  
    131.         bool isRunning = Input.GetKey(KeyCode.LeftShift);
    132.         float curSpeedX = canMove ? (isRunning ? runSpeed : walkSpeed) * Input.GetAxis("Vertical") : 0;
    133.         float curSpeedY = canMove ? (isRunning ? runSpeed : walkSpeed) * Input.GetAxis("Horizontal") : 0;
    134.         float movementDirectionY = moveDirection.y;
    135.         moveDirection = (forward * curSpeedX) + (right * curSpeedY);
    136.  
    137.         if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
    138.         {
    139.             moveDirection.y = jumpPower;
    140.         }
    141.         else
    142.         {
    143.             moveDirection.y = movementDirectionY;
    144.         }
    145.  
    146.         if (!characterController.isGrounded)
    147.         {
    148.             moveDirection.y -= gravity * Time.deltaTime;
    149.         }
    150.  
    151.         #region Rotation Handles
    152.         characterController.Move(moveDirection * Time.deltaTime);
    153.  
    154.         if (canMove)
    155.         {
    156.             rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
    157.             rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
    158.             playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
    159.             transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
    160.         }
    161.  
    162.         #endregion
    163.  
    164.         //Starting here, it's code for other things like grabbing stuff or manipulating stuff. Nothing relevant to the important for the movement.
    165.  
    166.         #region Pick System - Update
    167.         if (Input.GetMouseButtonDown(0))
    168.         {
    169.             // CHECK
    170.             if (CurrentObj)
    171.             {
    172.                 CurrentObj.useGravity = true;
    173.                 CurrentObj = null;
    174.                 isSauceTomato = false;
    175.                 return;
    176.             }
    177.  
    178.             // LAUNCH RAYCAST
    179.             Ray CamRay = playerCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    180.             if (Physics.Raycast(CamRay, out RaycastHit HitInfo, PickRange, PickMask))
    181.             {
    182.                 CurrentObj = HitInfo.rigidbody;
    183.                 CurrentObj.useGravity = false;
    184.  
    185.             }
    186.  
    187.             if (Physics.Raycast(CamRay, out RaycastHit HitInfo_pepperoni, PickRange, PickMask_Pepperoni))
    188.             {
    189.                 Pepperonimachinescript.inst.OnPressedPepperoni();
    190.                 Instantiate(Pepobj, TargetSpawnPp.transform.position, Pepobj.transform.rotation);
    191.             }
    192.  
    193.             if (Physics.Raycast(CamRay, out RaycastHit HitInfo_mushroom, PickRange, PickMask_Mushroom))
    194.             {
    195.                 Mushroommachinescript.inst.OnPressedMushroom();
    196.                 Instantiate(Mushobj, TargetSpawnMush.transform.position, Mushobj.transform.rotation);
    197.             }
    198.  
    199.             if (Physics.Raycast(CamRay, out RaycastHit HitInfo_blackolive, PickRange, PickMask_Olive))
    200.             {
    201.                 Bolivemachinescript.inst.OnPressedOlive();
    202.                 Instantiate(Oliveobj, TargetSpawnOlive.transform.position, Oliveobj.transform.rotation);
    203.             }
    204.  
    205.             if (Physics.Raycast(CamRay, out RaycastHit HitInfo_pineapple, PickRange, PickMask_Pineapple))
    206.             {
    207.                 Pineapplemachinescript.inst.OnPressedPineapple();
    208.                 Instantiate(Pineobj, TargetSpawnPine.transform.position, Pineobj.transform.rotation);
    209.             }
    210.  
    211.             if (Physics.Raycast(CamRay, out RaycastHit HitInfo_doughdispense, PickRange, PickMask_Dough))
    212.             {
    213.                 DoughDispense.inst.OnPressedDough();
    214.                 Instantiate(Doughobj, TargetSpawnDough.transform.position, Doughobj.transform.rotation);
    215.             }
    216.  
    217.  
    218.  
    219.  
    220.  
    221.             if (Physics.Raycast(CamRay, out RaycastHit HitInfo_Sauce, PickRange, PickMask))
    222.             {
    223.                 if (HitInfo.transform.tag == "TomatoSauce" || HitInfo.transform.tag == "HotSauce" || HitInfo.transform.tag == "SusSauce")
    224.                 {
    225.                     CurrentObj = HitInfo_Sauce.rigidbody;
    226.                     CurrentObj.useGravity = false;
    227.                     isSauceTomato = true;
    228.                 }
    229.             }
    230.  
    231.             if (isSauceTomato)
    232.             {
    233.                 IsTookSauceTomato = true;
    234.                 HitInfo_Sauce.transform.GetComponent<SauceScriptTomato>().CanRot = IsTookSauceTomato;
    235.             }
    236.             else if (!isSauceTomato)
    237.             {
    238.                 IsTookSauceTomato = false;
    239.             }
    240.         }
    241.  
    242.         //This
    243.  
    244.         Ray CamRayDETECT = playerCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    245.         if (Physics.Raycast(CamRayDETECT, out RaycastHit Hitinfo, PickRange, PickMask) && !CurrentObj
    246.         || Physics.Raycast(CamRayDETECT, out RaycastHit Hitinfo_peper, PickRange, PickMask_Pepperoni) && !CurrentObj
    247.         || Physics.Raycast(CamRayDETECT, out RaycastHit Hitinfo_mush, PickRange, PickMask_Mushroom) && !CurrentObj
    248.         || Physics.Raycast(CamRayDETECT, out RaycastHit Hitinfo_bolive, PickRange, PickMask_Olive) && !CurrentObj
    249.         || Physics.Raycast(CamRayDETECT, out RaycastHit Hitinfo_dough, PickRange, PickMask_Dough) && !CurrentObj
    250.         || Physics.Raycast(CamRayDETECT, out RaycastHit Hitinfo_pine, PickRange, PickMask_Pineapple) && !CurrentObj)
    251.         {
    252.             TargetCross.GetComponent<Image>().color = new Color32(43, 255, 0, 255);
    253.         }
    254.         else
    255.         {
    256.             TargetCross.GetComponent<Image>().color = new Color32(0, 0, 0, 255);
    257.         }
    258.  
    259.         if (CurrentObj)
    260.         {
    261.             TargetCross.GetComponent<Image>().color = new Color32(255, 0, 0, 255);
    262.         }
    263.  
    264.         if (isSauceTomato)
    265.         {
    266.             IsTookSauceTomato = true;
    267.             //HitInfo_Saucetom.transform.GetComponent<SauceScriptTomato>().CanRot = true;
    268.             //SauceScriptTomato.inst.CanRot = true;
    269.         }
    270.         else if (!isSauceTomato)
    271.         {
    272.             IsTookSauceTomato = false;
    273.             //HitInfo_Saucetom.transform.GetComponent<SauceScriptTomato>().CanRot = false;
    274.             //SauceScriptTomato.inst.CanRot = false;
    275.         }
    276.  
    277.         #endregion
    278.     }
    279. }
    280.  
     
  5. AngelHYYPE09

    AngelHYYPE09

    Joined:
    Jan 31, 2022
    Posts:
    15
    So there is improvement, the client now doesn't move like crazy when i try to move him in the inspector and can rotate the camera but he can only do it in the 'x' axis, plus he still can't move, it's stuck in the floor and also i can't move him in the inspector, he's just froze. So i think there's something that it's setting it's position to some value and doesn't let him move, or it may just be something that i'm doing wrong with the netcode.

    Btw, i also added a camera holder script to the player for him to set the camera position on the netcode and also for each player to have their own individual cameras. I don't know if that have something to do with the project, but just in case, here's the code for that:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using Unity.Netcode;
    6.  
    7. public class CameraNetHold : NetworkBehaviour
    8. {
    9.  
    10.     public GameObject Campos;
    11.     public Transform offsetobj;
    12.     [SerializeField] private Camera pcam;
    13.     public Vector3 offset;
    14.  
    15.  
    16.  
    17.     void Awake()
    18.     {
    19.        
    20.         pcam = GetComponent<Camera>();
    21.     }
    22.  
    23.  
    24.     public override void OnNetworkSpawn()
    25.     {
    26.         if (!IsOwner)
    27.         {
    28.             return;
    29.         }
    30.         base.OnNetworkSpawn();
    31.         Campos.SetActive(true);
    32.         Debug.Log("authoo");
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.         GoToTarget();
    39.     }
    40.  
    41.      public void GoToTarget()
    42.     {
    43.  
    44.         if (SceneManager.GetActiveScene().name == "SampleScene")
    45.         {
    46.             Campos.transform.position = offsetobj.position + offset;
    47.         }
    48.     }
    49. }
    50.  
     
  6. NoelStephens_Unity

    NoelStephens_Unity

    Unity Technologies

    Joined:
    Feb 12, 2022
    Posts:
    244
    You might be spawning the player into the floor... I would try just raising the y axis of the player prefab asset's default to like 1.0.
    Also, in your fixed update I would make this adjustment:

    Code (CSharp):
    1.     void FixedUpdate()
    2.     {
    3.         if (!IsOwner || !IsSpawned)
    4.         {
    5.             return;
    6.         }
    To prevent FixedUpdate from doing anything until it is spawned.
     
  7. AngelHYYPE09

    AngelHYYPE09

    Joined:
    Jan 31, 2022
    Posts:
    15
    IT WORKED! I don't know if it was that, but i saw that the player had the NetworkTransform component instead of the ClientNetworkTransform, maybe i changed it for testing and didn't realize, so i changed it and now it works like a charm!

    I've been struggling with this problem since the past few days, so thank you so much for your help! I owe you a big one!
     
  8. NoelStephens_Unity

    NoelStephens_Unity

    Unity Technologies

    Joined:
    Feb 12, 2022
    Posts:
    244
    Fantastic!
    Glad to hear this resolved your issue.
    ;)

    (If you think this completely resolves your issue then could you mark this as resolved?)
     
    Last edited: Nov 21, 2023
  9. AngelHYYPE09

    AngelHYYPE09

    Joined:
    Jan 31, 2022
    Posts:
    15