Search code examples
c#unity-game-enginetimermultiplayerphoton

Unity Photon timer synchronization


Im using Unity Photon for teamplay multiplayer game.

I have 2 teams A and B. There is also a zone on the map, if you get into it (only to one team), then the timer decreases from 10 seconds and when the value is 0, a certain action occurs. This zone works through OnTriggerStay. I have difficulties with this: When one player enters this zone, then everything is ok, the timer tends from 10 seconds to 0 at normal speed, BUT, when another player from the same team comes in, then the timer passes twice as fast. I need a normal multiplayer countdown timer, for this zone, not the x2 multi-seconds.

private double _timeToNewScore = 10f;
private double _time;
private bool hasCapturedOne

private List <Health> team1players;

private void OnTriggerStay(Collider other) 
{ 
 if (hasCapturedOne) 
 { 
   foreach (Health p in team1players) 
    { 
     ScorePointsFromPeriod(1); 
    }
 }
}

 private void ScorePointsFromPeriod(int team)
 {
         _time += Time.deltaTime; 
         if (_time >= _timeToNewScore)
         {
             DoSomething();
         }
 }

Please, help me to avoid multiply seconds in timer, if there 2 teammates in zone. thx


Solution

  • The way I would do this would be have some int that stores how many players are in your area, and check if there is at least one player there (you can use OnTriggerEnter() and OnTriggerExit() to increment/decrement your count.
    Then in an Update() method, decrease your timer if the number of players in your area is greater than 0