- Home /
Countdown in multiplayer game
Hello. I have a question for you guys here. Does anyone know how to make a countdown for multiplayer game? Like when you hit "Start", than the countdown starts. The other players should also see this countdown.
HOW TO MAKE THIS? Help please :)
Answer by whydoidoit · Jun 23, 2012 at 04:02 PM
When you connect to a player RPC them the Time.time of the server. They then store this on each player as a serverTimeOffset from their own Time.time `serverTimeOffset = serverTime - Time.time). Now the server can send an RPC with Do X at Y time, or the level ends at time t and the client just has to use the serverTimeOffset it has stored to work out what it means in terms of its Time.time.
Can you please make me a little script? I dont really understand how :( ... It would be very nice :)
The knowledge WDIDI has given there is PRICELESS.
You kids today are incredibly lucky you can find out stuff like this in 30 seconds, using the "internet".
When I was 13 it took literally months of travel, study, investigation, research, thought, etc, to figure that out.
It would be difficult for someone today to actually realise how hard it was to find stuff out before internet post boards.
That knowledge is incredibly valuable, you can literally make millions of euros just by knowing what he explains there. Very lucky world today
So for the server we will get an OnPlayerConnected() callback - use this to broadcast the time to the client. This script should be attached to whatever is your controlling scene object (something with a networkView that is in the scene, not instantiated at runtime). (Not tested)
LevelTimeControl.js
static var serverTimeOffset : float;
static var Instance : LevelTimeControl;
function Awake() {
Instance = this;
}
function OnPlayerConnected(player : NetworkPlayer) {
networkView.RPC("NotifyServerTime", player, Time.time);
}
@RPC
function NotifyServerTime(float time) {
serverTimeOffset = Time.time - time;
}
static function SyncedTime : float (serverTime : float) {
return Network.isServer ? time : time + serverTimeOffset;
}
static var levelEndTime : float;
@RPC
function SetLevelEndTime(timeFromNow : float) {
if(Network.isServer) {
levelEndTime = timeFromNow.Time.time;
networkView.RPC("SetLevelEndTime", RPC$$anonymous$$ode.Others, levelEndTime);
} else {
levelEndTime = timeFromNow + serverTimeOffset;
}
}
static function LevelTimeRemaining : float {
return levelEndTime - Time.time;
}
Now from another script something like:
function OnGUI() {
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.Label($$anonymous$$athf.Floor(LevelTimeControl.LevelTimeRemaining()).ToString());
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
}
And to set the level time on the server:
LevelTimeControl.Instance.SetLevelEndTime(90); // Level ends in 90 seconds
Answer by henry96 · Jun 23, 2012 at 02:27 PM
Make a variable and subtract it with Time.time. Time.time is a total time since scene loads. So by subtracting by it, it will give you a count down style.
Here is an example
var totalTime : int;
var timeLeft : int;
function Update () {
timeLeft = totalTime - Time.time;
print (timeLeft);
}
Hope this helps!
Do all players see the same countdown??? Also the players who join the server more late???
this wouldn't work @henry96 , nice try, but if you're playing online each person has different load times, that means whoever loads the scene fastest, their timer starts first. Therefore, in a racing game, say you load faster than the other guy, you get a head start on him.
Your answer
Follow this Question
Related Questions
Photon Multiplayer Countdown TImer 2 Answers
Unity networking tutorial? 6 Answers
UWP and HLAPI 0 Answers
Remote Actions parameters: NetworkInstanceId vs GameObject w/ netId 0 Answers