Hi, I created this script to manage a local coop game following the video below. As far as I manage to find out, the PlayerInputManager is somehow affecting where my players spawn. I have taken this video for reference, which has helped me a lot, but I can’t figure out how to fix this spawn position overwrite. I attach screenshots of the GameObject that manages this system.
using Cinemachine;
using System.Collections.Generic;
using UnityEngine.InputSystem;
using UnityEngine;
public class PlayerManager : MonoBehaviour
{
[SerializeField] private List<PlayerInput> players = new List<PlayerInput>();
[SerializeField] private List<Transform> spawnPoints;
[SerializeField] private List<LayerMask> playerLayers;
private PlayerInputManager playerInputManager;
private void Awake()
{
playerInputManager = FindObjectOfType<PlayerInputManager>();
}
private void OnEnable()
{
playerInputManager.onPlayerJoined += AddPlayer;
}
private void OnDisable()
{
playerInputManager.onPlayerJoined -= AddPlayer;
}
public void AddPlayer(PlayerInput player)
{
players.Add(player);
Transform playerParent = player.transform.parent;
playerParent.position = spawnPoints[players.Count - 1].position;
Debug.Log($"Player {players.Count} spawned at position {playerParent.position}");
// Convert the layer mask to an integer
int layerToAdd = (int)Mathf.Log(playerLayers[players.Count - 1].value, 2);
// Set the layer on the VirtualCameras
CinemachineVirtualCamera[] virtualCameras = playerParent.GetComponentsInChildren<CinemachineVirtualCamera>();
foreach (var virtualCamera in virtualCameras)
{
virtualCamera.gameObject.layer = layerToAdd;
Debug.Log($"Set layer {layerToAdd} for virtual camera {virtualCamera.name}");
}
// Configure the culling mask on the camera
Camera playerCamera = playerParent.GetComponentInChildren<Camera>();
playerCamera.cullingMask |= 1 << layerToAdd;
Debug.Log($"Set culling mask for player {players.Count}");
// Set the action in the custom Cinemachine Input Handler
CinemachineInputHandler[] inputHandlers = playerParent.GetComponentsInChildren<CinemachineInputHandler>();
var lookAction = player.actions.FindAction("Look");
foreach (var inputHandler in inputHandlers)
{
inputHandler.look = lookAction;
Debug.Log($"Set look action for input handler {inputHandler.name}");
}
}
}

