Browse Source

Camera will now track the average position of all players on the track, and catch up to the leading player if they are off the screen.

master
s3607057 (Angus Niven) 5 years ago
parent
commit
1cb4324a8c
1 changed files with 61 additions and 4 deletions
  1. +61
    -4
      Assets/Scripts/GameMode/ColorGameMode/RacetrackGameMode.cs

+ 61
- 4
Assets/Scripts/GameMode/ColorGameMode/RacetrackGameMode.cs View File

@ -35,7 +35,7 @@ public class RacetrackGameMode : GameMode
/// </summary>
protected override void OnRoundEnd(PlayerData[] allPlayers)
{
cameraCheck();
cameraCheck(allPlayers);
//At the end of each round, any stuck players are freed to resume moving next round
foreach (PlayerData player in allPlayers)
@ -51,10 +51,67 @@ public class RacetrackGameMode : GameMode
RoundCount++;
}
void cameraCheck()
void cameraCheck(PlayerData[] allPlayers)
{
//Get the average x-position of all players
float xAvg = 0.0f;
int livePlayerCount = 0;
foreach (PlayerData player in allPlayers)
{
if (player.client.Lives > 0)
{
xAvg += player.character.transform.position.x;
livePlayerCount++;
}
}
xAvg = xAvg / livePlayerCount;
//Turn that position into a vector in viewport space
Vector3 xAvgPos = new Vector3(xAvg, 0.0f, 0.0f);
Vector3 xAvgVP = Camera.main.WorldToViewportPoint(xAvgPos);
Debug.Log("xAvgPos = " + xAvgPos + ", xAvgVP = " + xAvgVP);
//We move the camera forward by at least one increment
//Keep doing it until the average x-position is roughly centred
do
{
Camera.main.transform.Translate(scrollSpeed, 0, 0, Space.World);
xAvgVP = Camera.main.WorldToViewportPoint(xAvgPos);
Debug.Log("Camera = " + Camera.main.transform.position + ", xAvgVP = " + xAvgVP);
} while (xAvgVP.x > 0.5f || xAvgVP.y > 0.5f);
//If the foremost player is off the screen, scroll forward to catch up with them
//Find who's furthest ahead
PlayerData playerAhead = allPlayers[0];
foreach (PlayerData player in allPlayers)
{
if (player.client.Lives > 0 && player.character.transform.position.x > playerAhead.character.transform.position.x)
{
playerAhead = player;
}
}
//Turn their position to a viewport vector
Vector3 playerVP = Camera.main.WorldToViewportPoint(playerAhead.character.transform.position);
//If that position is not in view, advance until it is
while (playerVP.x > 1 || playerVP.y > 1)
{
Camera.main.transform.Translate(scrollSpeed, 0, 0, Space.World);
playerVP = Camera.main.WorldToViewportPoint(playerAhead.character.transform.position);
}/**/
//We check for track sections we need to add/remove
mapManager.checkTrack();
//Move the camera forward at a steady rate each round
if (scrollSpeed > 0.0f)
/*if (scrollSpeed > 0.0f)
{
Camera.main.transform.Translate(scrollSpeed, 0, 0, Space.World);
mapManager.checkTrack();
@ -68,7 +125,7 @@ public class RacetrackGameMode : GameMode
else
{
//Debug.Log("Not scrolling");
}
}*/
}

Loading…
Cancel
Save