- Home /
Input not recognized after swapping control devices,Input being ignored (New Input System)
Hello everyone, thanks in advance for any tips or tricks you provide to me during this question.
I am using unity 2019.3.15f1, Input system 1.0.0.
Currently working on setting up aiming with-in a 2.5D game, it works exactly as intended which is nice but when swapping between inputs (gamepad/keyboard and mouse) it is having some issues.
When testing the game with the gamepad(right joystick) it will allow me to aim though once I have used the muse to to aim at all it will no longer allow the gamepad to be used for aiming.
Player Input component's debugger is showing that the joystick is still moving and registering a result but nothing is being detected within the game screen.
The issue seems directly linked to this set of code as all movement (left/right, jump and grapple) are still working with both gamepad and keyboard at any time, they are also in their own scripts.
I have tried printing the raw result from the input with:
controls.PlayerGround.Aiming.performed += context => print(context.ReadValue<Vector2>());
It worked on both devices but once again when the mouse was used the gamepad no longer would send out its results though would still work for movement... I am quite confused by this problem and any help would be appreciated.
using UnityEngine;
using UnityEngine.InputSystem;
public class AimController : MonoBehaviour
{
InputMaster controls;
[SerializeField] PlayerInput playerInput;
[SerializeField] Camera gameCam;
[Tooltip("Both should follow the same order, eg left first then right or vice versa")][Header("Devices requiring aiming")]
[SerializeField] Transform[] aimingDevice;
[SerializeField] Transform[] grappleDevice;
Vector2 aimDirection = Vector2.zero;
private void Update()
{
Aim(aimDirection);
}
private void Awake()
{
if (controls == null)
{
controls = new InputMaster();
}
controls.PlayerGround.Aiming.performed += context => aimDirection = context.ReadValue<Vector2>();
}
private void OnEnable()
{
controls.Enable();
}
private void OnDisable()
{
controls.Disable();
}
void Aim(Vector2 aimInput)
{
if (aimInput == Vector2.zero) { return; }
foreach (Transform device in aimingDevice)
{
if (device.GetComponent<LaserPointer>().IsOn())
{
AimDevice(device);
}
}
foreach (Transform device in grappleDevice)
{
AimDevice(device);
}
void AimDevice(Transform device)
{
if (playerInput.currentControlScheme == controls.KeyboardandMouseScheme.name)
{
AimAtMousePos();
}
if (playerInput.currentControlScheme == controls.GamepadScheme.name)
{
AimAtJoystickDirection();
}
// Local functions.
void AimAtMousePos()
{
Plane plane = new Plane(Vector3.forward, 0);
Ray rayCastStartPoint = gameCam.ScreenPointToRay(aimInput);
if (plane.Raycast(rayCastStartPoint, out float distance))
{
Vector3 screenPos = rayCastStartPoint.GetPoint(distance);
Vector3 difference = screenPos - device.position;
float rotation = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
device.rotation = Quaternion.Euler(0, 0, rotation);
}
}
void AimAtJoystickDirection()
{
float rotation = Mathf.Atan2(aimInput.y, aimInput.x) * Mathf.Rad2Deg;
device.rotation = Quaternion.Euler(0, 0, rotation);
}
}
}
}
Your answer
Follow this Question
Related Questions
joystick connected 1 Answer
Controller input not working correctly 0 Answers
Input for bluetooth controller? 0 Answers
Analog stick control 1 Answer
How can I rotate my player to look at the direction my Joystick is pointing to? (Top-Down 2D) 0 Answers