Hello
I need help rewriting input script
I’m using TARODEV’s 2D player input controller scripts, I very much like it’s feeling, and I’ve been adjusting it a lot for my own needs. I’ve managed to change everything I wanted but 1 thing. If I understand correctly, his script uses unity’s “new” Input System and gathers input FROM ALL CONTROLLER/GAMEPAD INSTANCES, so when I try adding other players using PlayerInputManager, 1 controller/gamepad controls every player that spawns. I would want to change it so that I can create local multiplayer
using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
namespace NewPlayerController
{
public class PlayerInput : MonoBehaviour
{
#if ENABLE_INPUT_SYSTEM
private NewPlayerInputActions _actions;
private InputAction _move, _jump, _dash;
private void Awake()
{
_actions = new NewPlayerInputActions();
_move = _actions.Player.Move;
_jump = _actions.Player.Jump;
_dash = _actions.Player.Dash;
}
private void OnEnable() => _actions.Enable();
private void OnDisable() => _actions.Disable();
public FrameInput Gather()
{
return new FrameInput
{
JumpDown = _jump.WasPressedThisFrame(),
JumpHeld = _jump.IsPressed(),
DashDown = _dash.WasPressedThisFrame(),
Move = _move.ReadValue<Vector2>()
};
}
#else
public FrameInput Gather()
{
return new FrameInput
{
JumpDown = Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.C),
JumpHeld = Input.GetKey(KeyCode.Space) || Input.GetKey(KeyCode.C),
DashDown = Input.GetKeyDown(KeyCode.X) || Input.GetMouseButtonDown(1),
Move = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"))
};
}
#endif
}
public struct FrameInput
{
public Vector2 Move;
public bool JumpDown;
public bool JumpHeld;
public bool DashDown;
}
}
I don’t know how much of the controller script would be needed to help me (since its 1k lines) so I’ll add some parts of the code that use input that should give some idea how things are working (I can add more in a reply if needed, I don’t know what is the character limit in this forum)
private PlayerInput _playerInput;
private FrameInput _frameInput;
private void Awake()
{
if (!TryGetComponent(out _playerInput)) _playerInput = gameObject.AddComponent<PlayerInput>();
if (!TryGetComponent(out _constantForce)) _constantForce = gameObject.AddComponent<ConstantForce2D>();
SetupCharacter();
PhysicsSimulator.Instance.AddPlayer(this);
}
private void GatherInput()
{
_frameInput = _playerInput.Gather();
if (_frameInput.JumpDown)
{
_jumpToConsume = true;
_timeJumpWasPressed = _time;
}
if (_frameInput.DashDown)
{
_dashToConsume = true;
}
}
private Vector2 _frameDirection;
private void CalculateDirection()
{
_frameDirection = new Vector2(_frameInput.Move.x, 0);
if (_grounded)
{
GroundNormal = _groundHit.normal;
var angle = Vector2.Angle(GroundNormal, Up);
if (angle < Stats.MaxWalkableSlope) _frameDirection.y = _frameDirection.x * -GroundNormal.x / GroundNormal.y;
}
_frameDirection = _frameDirection.normalized;
}
There might be a simple solution that I’m very much unaware off, I’m not using unity for that long so…
anyway
I hope someone sees a solution to my problem
thank you for you time in advance