je débute dans l'utilisation du nouveaux Input System et je rencontre un problème.
j'ai ajouter un GameObject qui contient un PlayerInput qui utilise des Unity Events et le script suivant afin de gérer les inputs:
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
[RequireComponent(typeof(PlayerInput))]
public class InputManager : MonoBehaviour
{
private Vector2 moveDirection = Vector2.zero;
private bool interactPressed = false;
private bool submitPressed = false;
private bool fishingPressed = false;
private static InputManager instance;
private bool pushPressed = false;
private void Awake()
{
if (instance != null)
{
Debug.LogError("Found more than one Input Manager in the scene.");
}
instance = this;
}
public static InputManager GetInstance()
{
return instance;
}
public void MovePressed(InputAction.CallbackContext context)
{
if (context.performed)
{
moveDirection = context.ReadValue<Vector2>();
}
else if (context.canceled)
{
moveDirection = context.ReadValue<Vector2>();
}
}
public void InteractButtonPressed(InputAction.CallbackContext context)
{
if (context.performed)
{
interactPressed = true;
}
else if (context.canceled)
{
interactPressed = false;
}
}
public void SubmitPressed(InputAction.CallbackContext context)
{
if (context.performed)
{
submitPressed = true;
}
else if (context.canceled)
{
submitPressed = false;
}
}
public Vector2 GetMoveDirection()
{
return moveDirection;
}
public void FishingPressed(InputAction.CallbackContext context)
{
if (context.performed)
{
fishingPressed = true;
}
else if (context.canceled)
{
fishingPressed = false;
}
}
public void PushPressed(InputAction.CallbackContext context)
{
if (context.performed)
{
pushPressed = true;
}
else if (context.canceled)
{
pushPressed = false;
}
}
// for any of the below 'Get' methods, if we're getting it then we're also using it,
// which means we should set it to false so that it can't be used again until actually
// pressed again.
public bool GetPushPressed()
{
bool result = pushPressed;
pushPressed = false;
return result;
}
public bool GetFishingPressed()
{
bool result = fishingPressed;
fishingPressed = false;
return result;
}
public bool GetInteractPressed()
{
bool result = interactPressed;
interactPressed = false;
return result;
}
public bool GetSubmitPressed()
{
bool result = submitPressed;
submitPressed = false;
return result;
}
public void RegisterSubmitPressed()
{
submitPressed = false;
}
}
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Fishing : MonoBehaviour
{
//fishing
[SerializeField] private GameObject fishingPanel;
bool canFish;
private PlayerInput playerInput;
[SerializeField] Transform topPivot;
[SerializeField] Transform bottomPivot;
[SerializeField] Transform fish;
[SerializeField] Transform capture;
float fishPosition;
float fishDestination;
float fishTimer;
[SerializeField] float timerMultiplicator = 3f;
float fishSpeed;
[SerializeField] float smoothMotion = 1f;
// Start is called before the first frame update
void Awake()
{
canFish = false;
fishingPanel.SetActive(false);
playerInput = GetComponent<PlayerInput>();
}
// Update is called once per frame
void Update()
{
//used for moving the fish
fishTimer -= Time.deltaTime;
if (fishTimer < 0f)
{
fishTimer = UnityEngine.Random.value * timerMultiplicator;
fishDestination = UnityEngine.Random.value;
}
fishPosition = Mathf.SmoothDamp(fishPosition, fishDestination, ref fishSpeed, smoothMotion);
fish.position = Vector3.Lerp(bottomPivot.position, topPivot.position, fishPosition);
if (InputManager.GetInstance().GetFishingPressed() && canFish)
{
fishingPanel.SetActive(true);
Debug.Log("you have press F and open the Panel");
//switch current actionMap
playerInput.SwitchCurrentActionMap("fishinggame");
Debug.Log("current action map : " + playerInput.currentActionMap.name);
if (InputManager.GetInstance().GetPushPressed())
{
Debug.Log("you have press e for pushing :");
}
}
if(!canFish)
{
fishingPanel.SetActive(false);
//Debug.Log("The panel is closed");
}
}
//fishing
public void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("fishingZone"))
{
Debug.Log("inside fishing zone");
canFish = true;
}
}
public void OnTriggerExit2D(Collider2D other)
{
if (other.CompareTag("fishingZone"))
{
Debug.Log("outside fishing zone");
canFish = false;
}
}
}
j'ai pour cela 2 ActionMaps (1 pour les deplacement normal et 1 pour les inputs lié à ce mini jeu)
J'arrive à changer l'ActionMap courante mais ensuite je n'arrive pas pas utiliser les inputs lié à celle-ci
Code : Tout sélectionner
if (InputManager.GetInstance().GetFishingPressed() && canFish)
{
fishingPanel.SetActive(true);
Debug.Log("you have press F and open the Panel");
//switch current actionMap
playerInput.SwitchCurrentActionMap("fishinggame");
Debug.Log("current action map : " + playerInput.currentActionMap.name);
if (InputManager.GetInstance().GetPushPressed())
{
Debug.Log("you have press e for pushing :");// Ne fonctionne pas
}
}