Search Unity

Prefab Move on the Host but Not on the Client (Unity NetCode)

Discussion in 'Netcode for GameObjects' started by mellasgdv, Dec 16, 2023.

  1. mellasgdv

    mellasgdv

    Joined:
    Jun 26, 2021
    Posts:
    6
    After Two DAYS researching on this topic I got Stocked :
    Unity version : 2023.2.3f1
    Netcode Version : 1.7.1

    this is the first time where i worked with netcode so am sorry for any stupid method i used.

    I tried to move the prefab on his own c# script after network spawned on the
    OnNetworkSpawn Function , but the same thing it move on the host but not on the client.

    and I tried invoking event action that stores all the info that i need for moving my prefab when the object spawned on the shooting system .cs and subscribe to it on the prefab script and trigger a move method with all the parameters from the event action but again the same thing.



    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using System;
    4. using Unity.Netcode;
    5. public class ShootingSystem : NetworkBehaviour
    6. {
    7.     [SerializeField] private Transform startPointTransform;
    8.     [SerializeField] private List<Weapon_SO> weapon_SOs = new List<Weapon_SO>();
    9.     [SerializeField] private float rangeModefier = 10;
    10.    
    11.     private int weaponIndex = 0;
    12.     [Range(0f,1f)]
    13.     private float holdTime = 0;
    14.    
    15.     private void Update()
    16.     {
    17.         if (!IsOwner) return;
    18.         if (InputsData.IsShootingStarted())
    19.         {
    20.             //start counting the holding time
    21.             if (holdTime <= 1)
    22.                 holdTime += Time.deltaTime;
    23.  
    24.         }else if (InputsData.IsShootingReleased())
    25.         {
    26.             //shoot
    27.             SpawnAndMovePrefabOnServerRpc(startPointTransform.position, Quaternion.identity);
    28.  
    29.             holdTime = 0;
    30.         }
    31.     }
    32.     [ServerRpc]
    33.     private void SpawnAndMovePrefabOnServerRpc(Vector3 pos, Quaternion rot)
    34.     {
    35.         GameObject InstantiatedObject = Instantiate(weapon_SOs[weaponIndex].projectilePrefab, pos, rot);
    36.         NetworkObject networkObject = InstantiatedObject.GetComponent<NetworkObject>();
    37.         networkObject?.Spawn();
    38.  
    39.         Move(networkObject.NetworkObjectId);
    40.         Destroy(InstantiatedObject, 3f);
    41.     }
    42.     void Move(ulong id)
    43.     {
    44.  
    45.         Transform cameraPosition = GetComponentInChildren<Camera>().transform;
    46.         Vector3 moveDirection = (cameraPosition.up * weapon_SOs[weaponIndex].upModefier + cameraPosition.forward * weapon_SOs[weaponIndex].forwadModefier)
    47.             * weapon_SOs[weaponIndex].range * rangeModefier * holdTime;
    48.  
    49.         GetNetworkObject(id)?.GetComponent<Rigidbody>().AddForce(moveDirection);
    50.     }
    51. }
    52.  



    i have another script for intercating with object and grabing them and it work fine and the both sides
     

    Attached Files:

    • 1.png
      1.png
      File size:
      270 KB
      Views:
      24
    • 2.png
      2.png
      File size:
      287.4 KB
      Views:
      24
    • 3.png
      3.png
      File size:
      190.7 KB
      Views:
      24
  2. NoelStephens_Unity

    NoelStephens_Unity

    Unity Technologies

    Joined:
    Feb 12, 2022
    Posts:
    258
    I would recommend looking at some of NGO's samples in the test project to see if that helps you with your project.
    The teleport sample shows one approach that uses the PlayerMovement script.
    You are free to borrow any of the scripts there and make adjustments as needed for your project.
     
    mellasgdv likes this.
  3. mellasgdv

    mellasgdv

    Joined:
    Jun 26, 2021
    Posts:
    6
    Thank you for your reply I Appreciate it . I will Try it Now
     
  4. NoelStephens_Unity

    NoelStephens_Unity

    Unity Technologies

    Joined:
    Feb 12, 2022
    Posts:
    258
    Also, here is a sample project I have used today answering some questions. It has the motion model aspect built into it and I thought you might find this useful as well.
     

    Attached Files: