fgm24/Assets/Scripts/Player/PlayerInput.cs

52 lines
1.4 KiB
C#
Raw Normal View History

2024-02-03 16:21:32 +01:00
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}");
}
}