- Home /
How do I update values and the transform of an object on the server's and client's side?
I'm making a very simple game, where the camera automatically scrolls down simply using gravity, and the players have to keep up with it. I'm sure you've all played a very similar game, where you have to go through holes in the floor to the next floor and the next until you can't keep up and lose.
Regardless, the issue I'm having is resulting from wanting to be able to restart the game at the touch of a key. Now, to do this, I've made a code to were the camera will return to it's original position, and it will disable gravity on it's Rigidbody, so the camera will stay in place until it's ready to scroll again. I also have included a few lines of code to move all the players to the original spawn point again, where they can then move from to start the game. The script looks like this:
function FixedUpdate () {
var camera = gameObject.Find("Camera").GetComponent.<Rigidbody>();
if(Input.GetKey("k"))
{
var players = GameObject.FindGameObjectsWithTag("Player");
for(var p = 0; p <players.length ; p++ )
{
players[p].transform.position = Vector3(0,3,-.6);
}
camera.useGravity = false;
camera.transform.position = Vector3(0, 0, -10);
}
}
Now, that works great, as long as there is only one player on the server. The camera goes to the desired location, and remains there until it's triggered again to fall, and the player is put into the right location.
The issue arises when another player connects.
If the player that is hosting presses the "K" key, the array for loop will gather all the GameObjects tagged as a player, and will move them to that position, and the camera will return up, on the server's screen, everything appears to be working fine.
But, only for a fraction of a second, Then, the client player that didn't depress the "K" key will begin bouncing between where they originally rested, and where the script called them too. On the client screen, the camera will still be moving downward, and the server will also be moving from where they were called to back to where they had originally rested. The camera will begin to shake and rubberband back up and down as the difference grows between the client and server.
So, I suppose the problem is, I need to be able to update the players'. and camera's transforms, and the camera settings on both the client and server side, so that the players and camera aren't in two different places on their respective screens. Does anyone know how to do so? A way to make both the client and server follow the same command? Both the players have NetworkView components attached to them, as does the empty object which holds the script I pasted above.
I'm sorry if any part of this was unclear, I'm not the best at articulating. If anyone needs anything cleared up, I can help. Thank you for any help I receive in advance