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.
using System.Collections.Generic;
using UnityEngine;
using System;
using Unity.Netcode;
public class ShootingSystem : NetworkBehaviour
{
[SerializeField] private Transform startPointTransform;
[SerializeField] private List<Weapon_SO> weapon_SOs = new List<Weapon_SO>();
[SerializeField] private float rangeModefier = 10;
private int weaponIndex = 0;
[Range(0f,1f)]
private float holdTime = 0;
private void Update()
{
if (!IsOwner) return;
if (InputsData.IsShootingStarted())
{
//start counting the holding time
if (holdTime <= 1)
holdTime += Time.deltaTime;
}else if (InputsData.IsShootingReleased())
{
//shoot
SpawnAndMovePrefabOnServerRpc(startPointTransform.position, Quaternion.identity);
holdTime = 0;
}
}
[ServerRpc]
private void SpawnAndMovePrefabOnServerRpc(Vector3 pos, Quaternion rot)
{
GameObject InstantiatedObject = Instantiate(weapon_SOs[weaponIndex].projectilePrefab, pos, rot);
NetworkObject networkObject = InstantiatedObject.GetComponent<NetworkObject>();
networkObject?.Spawn();
Move(networkObject.NetworkObjectId);
Destroy(InstantiatedObject, 3f);
}
void Move(ulong id)
{
Transform cameraPosition = GetComponentInChildren<Camera>().transform;
Vector3 moveDirection = (cameraPosition.up * weapon_SOs[weaponIndex].upModefier + cameraPosition.forward * weapon_SOs[weaponIndex].forwadModefier)
* weapon_SOs[weaponIndex].range * rangeModefier * holdTime;
GetNetworkObject(id)?.GetComponent<Rigidbody>().AddForce(moveDirection);
}
}
i have another script for intercating with object and grabing them and it work fine and the both sides