[RESOLU][new input system] script qui utilise 2 actionmap

Questions à propos du scripting. Hors Shader, GUI, Audio et Mobile.
tenkai1088
Messages : 1
Inscription : 26 Juin 2021 23:11

[RESOLU][new input system] script qui utilise 2 actionmap

Message par tenkai1088 » 04 Fév 2022 19:15

Bonsoir,
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;
    }
}
Et j'utilise ensuite le script suivant pour gérer les inputs pour une sorte de mini jeux de pêche:

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
            }
            
        }

Avatar de l’utilisateur
tenkai188
Messages : 34
Inscription : 29 Déc 2017 14:59

Re: script qui utilise 2 actionmap (new input system)

Message par tenkai188 » 10 Fév 2022 21:32

Personne ?
Je bloque la dessus depuis un moment :/
Si je me suis tromper de section pour ma question n'hésitez pas a me prévenir X|

Avatar de l’utilisateur
Max
Messages : 8772
Inscription : 30 Juil 2011 13:57
Contact :

Re: script qui utilise 2 actionmap (new input system)

Message par Max » 11 Fév 2022 19:42

Bonsoir,
tenkai188 a écrit :
10 Fév 2022 21:32
Personne ?
Perso je n'utilise pas le nouveau système d'Input, que je trouve bien trop lourdingue pour mes modestes besoin (et je pense que je ne suis pas le seul, d’où le peu de réponse à ton sujet). Donc pas évident de répondre quand on ne maitrise pas trop le sujet ;)

concernant ton soucis, déjà qu'est qui te fait penser que tu arrives bien à changer d'ActionMap ?

Après, tu cherches à tester deux touches (F et E si j'ai bien lu). Pourquoi passer par deux ActionMaps ?
Image
Pas d'aide par MP, le forum est là pour ça.
En cas de doute sur les bonnes pratiques à adopter sur le forum, consulter la Charte et sa FAQ

Avatar de l’utilisateur
tenkai188
Messages : 34
Inscription : 29 Déc 2017 14:59

Re: script qui utilise 2 actionmap (new input system)

Message par tenkai188 » 12 Fév 2022 01:48

ok
merci d'avoir pris de temps de me répondre :)
qu'est qui te fait penser que tu arrives bien à changer d'ActionMap ?
Dans le 2eme script que j'ai mis en ligne, j'utilise les 2 deux lignes de codes suivantes pour changer l'ActionMap et affiche le nom de l'ActionMap courante avec un Debug.Log qui me renvoie bien le nom de l'actionMap courante qui est bien celui de la 2eme et donc confirme que le changement à bien eu lieu:

Code : Tout sélectionner

//switch current actionMap
            playerInput.SwitchCurrentActionMap("fishinggame");
            Debug.Log("current action map : " + playerInput.currentActionMap.name);
Pourquoi passer par deux ActionMaps ?
mon projet est un actionrpg 2D top down et quand le perso est a porté d'une étendue d'eau, il à la possibilité de pêcher à l'aide d'une panel qui s'ouvre et propose un minijeux de pêche (assez proche dans le fonctionnement de celui proposé dans le jeu stardew valley par exemple)
lorsque ce mini jeux commence et que le panel s'ouvre je change l'action map afin d'éviter que toutes autres actions en dehors celles nécessaires soient exécutable et pouvoir ainsi exécuter essentiellement les actions lié au jeu de pêche
=> 1 action map pour gérer les actions "normales" et 1 pour celle lié à la pêche.

Avatar de l’utilisateur
Max
Messages : 8772
Inscription : 30 Juil 2011 13:57
Contact :

Re: script qui utilise 2 actionmap (new input system)

Message par Max » 12 Fév 2022 10:51

tenkai188 a écrit :
12 Fév 2022 01:48
Dans le 2eme script que j'ai mis en ligne, j'utilise les 2 deux lignes de codes suivantes pour changer l'ActionMap et affiche le nom de l'ActionMap courante[/code]
à oui, autant pour moi je n'avais pas fait attention.

par contre, une fois que tu as changé d'ActionMap, tu n’entreras plus dans ta condition
if (InputManager.GetInstance().GetFishingPressed() && canFish), et donc le code:

Code : Tout sélectionner

            if (InputManager.GetInstance().GetPushPressed())
            {
                Debug.Log("you have press e for pushing :");// Ne fonctionne pas
            }
            
ne sera jamais executé.
Image
Pas d'aide par MP, le forum est là pour ça.
En cas de doute sur les bonnes pratiques à adopter sur le forum, consulter la Charte et sa FAQ

Avatar de l’utilisateur
tenkai188
Messages : 34
Inscription : 29 Déc 2017 14:59

Re: [new input system] script qui utilise 2 actionmap

Message par tenkai188 » 12 Fév 2022 19:00

ok, j'ai modifier le script:

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;

    bool canpush;


    // Start is called before the first frame update

    void Awake()
    {
        canFish = false;
        canpush = 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
            Debug.Log("current action map 1 : " + playerInput.currentActionMap.name);
            playerInput.SwitchCurrentActionMap("fishinggame");
            Debug.Log("current action map 2 : " + playerInput.currentActionMap.name);
            canpush = true;
   
        }


        if (InputManager.GetInstance().GetInteractPressed())
        {
            Debug.Log("current action map 3 : " + playerInput.currentActionMap.name);

            Debug.Log("you have press e for interact :");
            Debug.Log("canpush:"+canpush);
        }

        if (InputManager.GetInstance().GetPushPressed())
        {
           
            Debug.Log("you have press e for pushing :");// Ne fonctionne pas
        }

    }

    //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;
        }
    }
  
}
Et j'ai ajouter les lignes de code suivante qui mon permis de trouver un autre problème:
Le changement de l'action map s'effectue comme il faut car

Code : Tout sélectionner

 Debug.Log("current action map 3 : " + playerInput.currentActionMap.name);
renvoie bien la nouvelle map "fishinggame" mais bizarrement il gère les inputs avec les actions de l'ancienne action map("game") car ce if fonctionne:

Code : Tout sélectionner

if (InputManager.GetInstance().GetInteractPressed())
        {
            Debug.Log("current action map 3 : " + playerInput.currentActionMap.name);

            Debug.Log("you have press e for interact :");
            Debug.Log("canpush:"+canpush);
        }
alors que c'est celui-ci qui devrait être exécuter:

Code : Tout sélectionner

        if (InputManager.GetInstance().GetPushPressed())
        {
           
            Debug.Log("you have press e for pushing :");// Ne fonctionne pas
        }

Avatar de l’utilisateur
Max
Messages : 8772
Inscription : 30 Juil 2011 13:57
Contact :

Re: [new input system] script qui utilise 2 actionmap

Message par Max » 12 Fév 2022 19:21

tu as vérifié tes events au niveau de tes ActionMap dans le PlayerInput ?
Image
Pas d'aide par MP, le forum est là pour ça.
En cas de doute sur les bonnes pratiques à adopter sur le forum, consulter la Charte et sa FAQ

Avatar de l’utilisateur
tenkai188
Messages : 34
Inscription : 29 Déc 2017 14:59

Re: [new input system] script qui utilise 2 actionmap

Message par tenkai188 » 12 Fév 2022 20:03

Problème résolu :super:
J'ai comme tu me la conseillé vérifié les events et je me suis rendu compte que j'avais 2 player input :gene:
un dans mon manager et un dans Player (mon gameobject représentant le joueur) ce qui engendrait un conflits
Encore merci pour ton aide :)

Répondre

Revenir vers « Scripting »