49 lines
1.3 KiB
C#
49 lines
1.3 KiB
C#
using System;
|
|
using Unity.Cinemachine;
|
|
using UnityEngine;
|
|
|
|
public class PlayerSwitcher : MonoBehaviour
|
|
{
|
|
[SerializeField] private CinemachineCamera cam;
|
|
[Header("Players")]
|
|
[SerializeField] private Player player1;
|
|
[SerializeField] private AutomatedPlayer player1AI;
|
|
[SerializeField] private Player player2;
|
|
[SerializeField] private AutomatedPlayer player2AI;
|
|
|
|
[Header("Switch Cooldowns")]
|
|
[SerializeField] private float switchCooldown;
|
|
private float currentSwitchCooldown;
|
|
|
|
|
|
private void Update()
|
|
{
|
|
if (Input.GetKeyDown(KeyCode.E) && currentSwitchCooldown <= 0f)
|
|
{
|
|
if (player1.isActiveAndEnabled)
|
|
{
|
|
SwitchPlayers(player1, player1AI, player2, player2AI);
|
|
}
|
|
else
|
|
{
|
|
SwitchPlayers(player2, player2AI, player1, player1AI);
|
|
}
|
|
currentSwitchCooldown = switchCooldown;
|
|
}
|
|
|
|
if (currentSwitchCooldown > 0f)
|
|
{
|
|
currentSwitchCooldown -= Time.deltaTime;
|
|
}
|
|
}
|
|
|
|
public void SwitchPlayers(Player playerA, AutomatedPlayer playerAAI, Player playerB, AutomatedPlayer playerBAI)
|
|
{
|
|
cam.Follow = playerB.transform;
|
|
playerAAI.enabled = true;
|
|
playerA.enabled = false;
|
|
playerB.enabled = true;
|
|
playerBAI.enabled = false;
|
|
(playerB.transform.position, playerAAI.transform.position) = (playerAAI.transform.position, playerB.transform.position);
|
|
}
|
|
}
|