Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    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:
    154
    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. public void ShowTrailServerRpc(Vector3 startPos, Vector3 hitpoint)
    2.     {
    3.        StartCoroutine(ShowTrail(startPos, hitpoint));
    4.     }
    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.  
    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 7, 2023