#Spawning and Syncing Objects
1 messages · Page 1 of 1 (latest)
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.
one more thing tho for player1 his tanks should be blue since they are friendly and the player2 tanks should be red for player 1 since they are enemies. and for player2 the tanks should be the opposite color
@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
}
}
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()
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.
Each Tank should have their own script, something like TankController. That should store any data about the tank and contain any functions that relate to the tank, like movement.
Why do you need to attach a script to something at runtime? This is almost never needed or good practice.
My bad i did not think of prefabs
Btw later is it like possible to make the units invisible when i am going to implement spotting
Easiest way is to probably make a Prefab called Tank that has your TankController script on it.
Then put the actual model of the tank as a child.
This way you can turn the model on and off when you want to to make it invisible, without turning off the parent and interrupting anything TankController is doing or interrupting the NetworkTransform that syncs the tanks position.