Add player joining
In the platformer scene, whenever you connect a new keyboard or controller, or interact with one already connected, a new player will be created and tied to that input device. Please try to break this system to make sure it works properly.
This commit is contained in:
31
Assets/PlayerManager.cs
Normal file
31
Assets/PlayerManager.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public class PlayerManager : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Vector2 spawnPosition;
|
||||
|
||||
private PlatformerCameraMovement playerCamera;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
GetComponent<PlayerInputManager>().onPlayerJoined += OnPlayerJoined;
|
||||
GetComponent<PlayerInputManager>().onPlayerLeft += OnPlayerLeft;
|
||||
|
||||
playerCamera = FindFirstObjectByType<PlatformerCameraMovement>();
|
||||
}
|
||||
|
||||
private void OnPlayerJoined(PlayerInput playerInput)
|
||||
{
|
||||
playerInput.transform.position = spawnPosition;
|
||||
playerCamera.players.Add(playerInput.gameObject);
|
||||
print("Player joined");
|
||||
}
|
||||
|
||||
private void OnPlayerLeft(PlayerInput playerInput)
|
||||
{
|
||||
Destroy(playerInput.gameObject);
|
||||
playerCamera.players.Remove(playerInput.gameObject);
|
||||
print("Player left");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user