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.
  2. Dismiss Notice

Resolved synch spawned objects

Discussion in 'Netcode for GameObjects' started by liambilly, Jul 7, 2023.

  1. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    86
    hello guys i have i have a projectile, when a player spawns it, it is moved by the networkbehavior attached to it
    it works on the hostside, the problem is on the client it doesn't the projectile just travels downwards n not towards the hit.point, lemme show you the codes
    Code (CSharp):
    1. [ServerRpc(RequireOwnership = false)]
    2.     public void ShowTrailServerRpc(Vector3 startPos, Vector3 hitpoint)
    3.     {
    4.         StartCoroutine(ShowTrail(startPos, hitpoint));
    5.     }
    6.     public IEnumerator ShowTrail(Vector3 startPos, Vector3 hitpoint)
    7.     {
    8.         yield return new WaitUntil(() => startPos != Vector3.zero);
    9.         float time = 0;
    10.         while (time < 1)
    11.         {
    12.             transform.position = Vector3.Lerp(startPos, hitpoint, time);
    13.             time += Time.deltaTime / trail.time;
    14.             yield return null;
    15.         }
    16.         //hit effect;
    17.     }    
    18. // I have this code in a networkbehavior attached to a bullet, and is called when the player //spawns it
    19.  
     
    Last edited: Jul 7, 2023
  2. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    86
    and the code the player uses to spawn this projectile

    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (!IsOwner) return;
    4.         Rotate();
    5.         if (Physics.Raycast(playerCam.position, playerCam.forward, out hit, 1000, ~layerMask))
    6.         {
    7.             Debug.DrawLine(playerCam.position, hit.point, OwnerClientId == 1 ? Color.red : Color.green);
    8.         }
    9.         if (Input.GetKeyDown(KeyCode.Mouse0))
    10.         {
    11.             if (Physics.Raycast(playerCam.position, playerCam.forward, out hit, 1000, ~layerMask))
    12.             {
    13.                 dirToShoot = (hit.point - transform.position).normalized;
    14.                 ProjectileServerRpc(dirToShoot);
    15.                 print(hit.transform);
    16.             }
    17.         }
    18.     }
    19.     [ServerRpc(RequireOwnership = false)]
    20.     void ProjectileServerRpc(Vector3 direction)
    21.     {
    22.         Projectile2(direction);
    23.     }
    24.     void Projectile2(Vector3 direction)
    25.     {
    26.         GameObject go = Instantiate(prefab, startpos.position, startpos.rotation);
    27.         go.GetComponent<NetworkObject>().Spawn();
    28.         go.GetComponent<TestProjectile>().ShowTrailServerRpc(startpos.position, hit.point);
    29.     }
     
    Last edited: Jul 8, 2023
  3. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    218
    You start the taril corutine only on the server. Shouldn't this be client rpc like:
    Code (CSharp):
    1. [ClientRPC]
    2.     public void ShowTrailClientRpc(Vector3 startPos, Vector3 hitpoint)
    3.     {
    4.         StartCoroutine(ShowTrail(startPos, hitpoint));
    5.     }
    6.     public IEnumerator ShowTrail(Vector3 startPos, Vector3 hitpoint)
    7.     {
    8.         yield return new WaitUntil(() => startPos != Vector3.zero);
    9.         float time = 0;
    10.         while (time < 1)
    11.         {
    12.             transform.position = Vector3.Lerp(startPos, hitpoint, time);
    13.             time += Time.deltaTime / trail.time;
    14.             yield return null;
    15.         }
    16.         //hit effect;
    17.     }  
     
  4. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    86
    i tried calling the clientrpc too instead of serverrpc from the player's script it behaved the same way, still just working on the server,
     
  5. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    218
    Do you have netwrok transform on the bullet? It will make the position sync between the client and the server. Or you can create a bullet in the clientRPC and move it localy.
     
  6. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    86
    i found the solution using networkobject refrence