I’m trying to do a simple scene where I can control 4 players.
I need help setting up this because every bit of explanation is out of date today.
This script below is attached to a simple cube prefab.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private InputMaster controls;
[SerializeField] private float speed = 10;
private void Awake()
{
controls = new InputMaster();
controls.Player.Shoot.performed += _ => Shoot();
controls.Player.Move.performed += ctx => Move(ctx.ReadValue<Vector2>());
}
private void OnEnable()
{
controls.Enable();
}
private void OnDisable()
{
controls.Disable();
}
private void Shoot()
{
Debug.Log("Shoot");
}
private void Move(Vector2 direction)
{
transform.position += new Vector3(
direction.x * speed * Time.deltaTime,
0,
direction.y * speed * Time.deltaTime);
}
}
I have a Player Input Manager in my scene with the Player prefab (the cube) attached to it.
I have set up my Input Actions to take inputs from the keyboard and from game pads.
Okay, so now I have one keyboard and two controllers. When I touch the keyboard, one player (cube) appears and respond to the controls. Now if I touch a controller, another player appears, but every player is controlled by every controller & keyboard.
Can someone try and help me please ? I’m completely lost in this new Input System, but I want it to work so bad
yes there is a bit of confusion around this. What is happening in your script is the PlayerInput is being created multiple times and that is why each controller controls each cube. The easiest way to do this. Create your cube prefab. Add a Player Input component on the prefab. Add a script for movement. Now in the script the only input code you want to add is something like
This will all be taken care of under the hood. Now in your scene create an empty game object and add a Player Input manager component. Set the Player prefab to your cube. Now it should just work. hit a button on gamepad 1 and a cube will spawn etc. only one input device will be mapped to each spawn of the cube.
If you can’t get it working I will attach a small sample project.
Add this to your script to pick up the InputValue using UnityEngine.InputSystem.Plugins.PlayerInput;
(I am assuming your using the latest package 0.2.10-preview)
Take a look at this project. Very little script code. 1. Create your prefab. 2. Add a Player Input component to the prefab. 3. Looking at your actions asset react to the event in your script, ie if you have a Move in your action map, have an OnMove. 4. Globally add a Player Input Manager to a game object. 5. In the Player Input Manager have the prefab as the Player Input. Let me know if you have any questions on this project.
@dougpunity3d so you’re not supposed to use the file with the generated code if you want to do local multiplayer? I tried that all weekend but could not get it to work Any plans when that will be possible?