Files
Crash-Course/Assets/Scripts/Player/PlayerCameraMovement.cs

87 lines
2.6 KiB
C#
Raw Normal View History

2025-01-10 16:14:33 -05:00
using System.Collections.Generic;
using UnityEngine;
public class PlayerCameraMovement : MonoBehaviour
2025-01-10 16:14:33 -05:00
{
private Vector3 start;
private Vector3 target;
public float weight;
public float speed;
2025-03-07 10:03:16 -05:00
private GameObject playerThatWon;
2025-03-08 21:04:15 -05:00
public float lowerBound;
2025-03-07 10:03:16 -05:00
public bool winScene = false;
2025-01-10 16:14:33 -05:00
2025-03-31 19:33:31 -04:00
public bool staticCamera = false;
2025-01-10 16:14:33 -05:00
private void Start()
{
start = transform.position;
}
private void Update()
{
if (winScene) // If the game is over, the camera will follow the player that won
2025-03-07 10:03:16 -05:00
{
2025-03-07 11:56:19 -05:00
if (playerThatWon == null || !playerThatWon.activeInHierarchy)
{
playerThatWon = FindWinner();
}
if (playerThatWon != null)
{
target = playerThatWon.transform.position;
2025-03-08 13:33:19 -05:00
transform.position = Vector3.Lerp(transform.position, new Vector3(target.x, target.y, target.z - 10), speed * 12 * Time.deltaTime);
2025-03-10 16:23:24 -04:00
if (transform.position.y < lowerBound)
{
transform.position = new Vector3(transform.position.x, lowerBound, transform.position.z);
}
2025-03-07 11:56:19 -05:00
}
2025-03-07 10:03:16 -05:00
return;
}
// Moves the camera to follow the players
2025-02-19 20:11:57 -05:00
List<GameObject> players = GameManager.players;
if (players.Count == 0) return;
2025-01-10 16:14:33 -05:00
Vector3 playerAverage = Vector3.zero;
2025-03-07 11:56:19 -05:00
int activePlayers = 0;
2025-01-10 16:14:33 -05:00
foreach (GameObject player in players)
{
2025-03-07 11:56:19 -05:00
if (player == null || !player.activeInHierarchy) continue;
Damageable damageable = player.GetComponent<Damageable>();
if (damageable != null && damageable.dying) continue;
2025-01-10 16:14:33 -05:00
playerAverage += player.transform.position;
2025-03-07 11:56:19 -05:00
activePlayers++;
2025-01-10 16:14:33 -05:00
}
2025-03-07 11:56:19 -05:00
if (activePlayers == 0) return;
2025-03-31 19:33:31 -04:00
if (staticCamera) return;
2025-03-07 11:56:19 -05:00
playerAverage /= activePlayers;
2025-01-10 16:14:33 -05:00
target = start * weight + playerAverage * (1 - weight);
2025-02-17 19:14:53 -05:00
transform.position = Vector3.Lerp(transform.position, new Vector3(target.x, target.y, transform.position.z), speed * Time.deltaTime);
2025-03-10 16:23:24 -04:00
if (transform.position.y < lowerBound)
{
transform.position = new Vector3(transform.position.x, lowerBound, transform.position.z);
}
2025-01-10 16:14:33 -05:00
}
2025-03-07 10:03:16 -05:00
public void WinScene(GameObject player)
{
winScene = true;
playerThatWon = player;
}
2025-03-07 11:56:19 -05:00
private GameObject FindWinner() // Finds the player that won
2025-03-07 11:56:19 -05:00
{
foreach (GameObject player in GameManager.players)
{
if (player != null && player.activeInHierarchy)
{
return player;
}
}
return null;
}
2025-01-10 16:14:33 -05:00
}