#Spawning and Syncing Objects

1 messages · Page 1 of 1 (latest)

zinc apex
#

This is what NetworkTransform is for.

#

Syncing the position of GameObjects.

#

Spawning and Syncing Objects

#

@rustic cosmos the issue of having the local player be at the bottom and the enemy be at the top is a visual issue, and does not require you to manipulate the actual position of the tanks or the base.

#

Just use two different cameras.

#
    Xc
    X    

    Y
    Yc

X = Player1
Xc = Player1 Camera

Y = Player2
Yc = Player2 Camera

#

From both their perspectives they'll be at the bottom of the screen. And that way you can use GameObjects for the Tanks with NetworkTransforms and they'll always have the same position for every player. Which will make your life so much easier.

rustic cosmos
zinc apex
#

@rustic cosmos

[Rpc(SendTo.Server)]
public void AddTankRpc(ulong clientId)
{
  var tank = Instantiate(tankPrefab);

  // perform any custom setup to tank here

  var tankNetworkObject = tank.GetComponent<NetworkObject>();
  tankNetworkObject.SpawnWithOwnership(clientId);

  allTanks.Add(tankNetworkObject);
} ```

That's why I reccomended this approach. When a client requests the server to spawn a tank, the server spawns it and gives ownership to the client that asked for it to be spawned. 

Then inside of the `Tank` script you can have on each Tank you can simply perform code something like this:

```cs
public void override OnNetworkSpawn()
{
  var localClientId = NetworkManager.LocalClientId;

  if (OwnerClientId == localClientId)
  {
    // make tank blue
  }

  else
  {
    // make tank red
  }
}
rustic cosmos
#

okay

#

thank you will update you if i succeed with this approach 👍

zinc apex
#

Simpler is always better!

#

Breaking up things in to different scripts and functions will help you as you go along.

#

and try to stay out of Update() unless you absolutely have to use it.

#

at the very least put things like movement in to a function and then call that function in Update()

rustic cosmos
# zinc apex Simpler is always better!

I have some more questions tho. Will there not be be the same problem with allTanks when i try to access it in update to move the tanks. Also where should i store the data related to the unit for example: the data that says where the player has told the tank to move (desx,desy) or the data that tells how long on the reload the tank has left (reload). And lastly how do i attach a script to a game object from a script. Last time i tried the game didnt give me any errors but refused to start so i probably did it wrong.

zinc apex
#

Why do you need to attach a script to something at runtime? This is almost never needed or good practice.

rustic cosmos
rustic cosmos
zinc apex