Search Unity

Debugging New Input System / Gamepad Controller / Animator

Discussion in 'Input System' started by theBertv113, Aug 30, 2020.

  1. theBertv113

    theBertv113

    Joined:
    Apr 19, 2019
    Posts:
    2
    Disclaimer: I've only been working with Unity for about 6 months, so please be kind.

    I'm working on a player controller script utilizing the new UnityEngine.InputSystem, and I've come across a problem when implementing an animator with a gamepad controller input.

    My problem lies mostly within the code, which I will attach at the end, but when I hit start, the script is immediately taking in a value and triggering the WalkBack_Shoot_AR animation and is never returning to the Idle_gunMiddle_AR animation. I'm wondering if the new InputSystem is continuously sending a value to my move.x variable causing it to trip the animation, hence my need to figure out a way to debug. Most other forum posts I've seen are centered around PC controls, hence my dilemma. Thanks, in advance, to any and all who can help me with this problem.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     [SerializeField] private Animator _playerAnimator;
    9.     [SerializeField] private float _walkingSpeed = 3.1f;
    10.     [SerializeField] private float _lookSensitivity = 96f;
    11.     [SerializeField] private float _moveSensitivity = 0.2f;
    12.     //[SerializeField] private float _runningSpeed = 6.2f;
    13.  
    14.     private PlayerControls controls;
    15.     private Vector2 _move;
    16.     private Vector2 _look;
    17.     private bool _isWalkingForward = false;
    18.     private bool _isWalkingBackward = false;
    19.     private bool _isWalkingLeft = false;
    20.     private bool _isWalkingRight = false;
    21.     private bool _isShooting = false;
    22.  
    23.     private void Awake()
    24.     {
    25.         controls = new PlayerControls();
    26.         _playerAnimator = GetComponent<Animator>();
    27.  
    28.         controls.Gameplay.Move.performed += ctx => _move = ctx.ReadValue<Vector2>();
    29.         controls.Gameplay.Move.canceled += ctx => _move = Vector2.zero;
    30.  
    31.         controls.Gameplay.Look.performed += ctx => _look = ctx.ReadValue<Vector2>();
    32.         controls.Gameplay.Look.canceled += ctx => _look = Vector2.zero;
    33.     }
    34.  
    35.     private void OnEnable()
    36.     {
    37.         controls.Gameplay.Enable();
    38.     }
    39.  
    40.     private void OnDisable()
    41.     {
    42.         controls.Gameplay.Disable();
    43.     }
    44.  
    45.     private void FixedUpdate()
    46.     {
    47.         CalculateMovement();
    48.     }
    49.  
    50.     private void CalculateMovement()
    51.     {
    52.         Vector3 m = new Vector3(_move.x, 0, _move.y) * _walkingSpeed * Time.deltaTime;
    53.         transform.Translate(m, Space.World);
    54.         Vector3 l = new Vector3(0, _look.x, 0) * _lookSensitivity * Time.deltaTime;
    55.         transform.Rotate(l, Space.World);
    56.  
    57.         CharacterAnimation();
    58.     }
    59.  
    60.     private void CharacterAnimation()
    61.     {
    62.         _playerAnimator.SetBool("isWalkingForward", _isWalkingForward);
    63.         _playerAnimator.SetBool("isWalkingBackward", _isWalkingBackward);
    64.         _playerAnimator.SetBool("isWalkingLeft", _isWalkingLeft);
    65.         _playerAnimator.SetBool("isWalkingRight", _isWalkingRight);
    66.  
    67.         _isWalkingForward = (_move.x > _moveSensitivity) ? true : false;
    68.         _isWalkingBackward = (_move.x < _moveSensitivity) ? true : false;
    69.         _isWalkingLeft = (_move.y < _moveSensitivity) ? true : false;
    70.         _isWalkingRight = (_move.y > _moveSensitivity) ? true : false;
    71.     }
    72. }
    UnityAnimatorCapture.PNG