Having two input devices that follow the cursor's rotation with unity's new input system

Viewed 287

So I am creating a top down shooter game and have just started using unity's new input system. I currently have it set to where you can use the mouse or a controller's right stick to rotate the player's gun. The right stick works but, it causes you not to be able to use the mouse rotation anymore. It does this even when the controller is plugged out of the PC but, if I completely remove the controller part of the script the mouse rotation starts working again. Here is a picture of my script.

 Vector2 mouseScreenPosition = playerControls.Player.MousePosition.ReadValue<Vector2>();//Allows the mouse to follow the cursors rotation
    Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(mouseScreenPosition);
    Vector3 targetDirection = mouseWorldPosition - transform.position;
    float angle = Mathf.Atan2(targetDirection.y, targetDirection.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, angle));

    Vector2 p = playerControls.Player.Rotate.ReadValue<Vector2>();//Allows the right stick of a gamepad to follow the mouse cursor
    playerControls.Player.Rotate.canceled += ctx => p = Vector2.zero;
    Vector3 gamePadCam = Camera.main.ScreenToWorldPoint(p);
    Vector3 gamePadDirection = gamePadCam - transform.position;
    float gamePadangle = Mathf.Atan2(p.y, p.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, gamePadangle));
0 Answers
Related