52 lines
1.4 KiB
C#
52 lines
1.4 KiB
C#
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.InputSystem;
|
||
|
|
||
|
public class PlayerInput : MonoBehaviour
|
||
|
{
|
||
|
[SerializeField] private int playerNumber;
|
||
|
|
||
|
public Vector2 movement;
|
||
|
public Gamepad controller { get; private set; }
|
||
|
|
||
|
public bool whipAttack;
|
||
|
public event Action ropeLengthShrinken;
|
||
|
public event Action ropeLengthExtend;
|
||
|
|
||
|
public int PlayerNum => playerNumber;
|
||
|
|
||
|
private void Awake()
|
||
|
{
|
||
|
controller = Gamepad.all.ElementAtOrDefault(playerNumber);
|
||
|
if (controller == null)
|
||
|
{
|
||
|
Debug.LogWarning($"No Gamepad found for player {playerNumber + 1}");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void Update()
|
||
|
{
|
||
|
if (controller != null)
|
||
|
{
|
||
|
movement.x = controller.leftStick.x.ReadValue();
|
||
|
movement.y = controller.leftStick.y.ReadValue();
|
||
|
|
||
|
whipAttack = controller.buttonWest.IsPressed();
|
||
|
|
||
|
if (controller.rightShoulder.IsPressed()) ropeLengthShrinken?.Invoke();
|
||
|
if (controller.leftShoulder.IsPressed()) ropeLengthExtend?.Invoke();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
movement.x = Input.GetAxisRaw("Horizontal");
|
||
|
movement.y = Input.GetAxisRaw("Vertical");
|
||
|
|
||
|
whipAttack = Input.GetKey(KeyCode.B);
|
||
|
}
|
||
|
//Debug.Log($"player {playerNumber}: move {movement}");
|
||
|
}
|
||
|
}
|