How can I treat mouse input in Unity for an FPS as a Vector2 like Input.GetAxis or a virtual joystick

Viewed 44

Hi StackOverflow community, I have one common issue and need your help. I am a newbie in Unity but trying each day to improve my knowledge, sooo... Recently, I completed one of the Udemy courses and now trying to add new features and run the game on a Mobile device. I added a Virtual joystick asset and already changed movement with that Joystick. But for aim functionality, it is hard for me to change to Joystick.

What I changed already:

//moveInput.x = Input.GetAxisRaw("Horizontal"); // PC
//moveInput.y = Input.GetAxisRaw("Vertical"); // PC

moveInput.x = movingJoystick.Horizontal; // Mobile
moveInput.y = movingJoystick.Vertical; // Mobile

But now I need to change Vector3 mousePos = Input.mousePosition; to something similar to weapon.Joystic.Horizonral as I did with movement.

Please, advise some good implementations.

Here is a part of the code that handles character's and weapon rotation:

if (canMove && !LevelManager.instance.isPaused)
        {
            //moveInput.x = Input.GetAxisRaw("Horizontal"); // PC
            //moveInput.y = Input.GetAxisRaw("Vertical"); // PC

            moveInput.x = movingJoystick.Horizontal; // Mobile
            moveInput.y = movingJoystick.Vertical; // Mobile
            
            moveInput.Normalize();

            //transform.position += new Vector3(moveInput.x * Time.deltaTime * moveSpeed, moveInput.y * Time.deltaTime * moveSpeed, 0f);

            theRB.velocity = moveInput * activeMoveSpeed;

            Vector3 mousePos = Input.mousePosition; 

            Vector3 screenPoint = CameraController.instance.mainCamera.WorldToScreenPoint(transform.localPosition);

            if (mousePos.x < screenPoint.x)
            {
                transform.localScale = new Vector3(-1f, 1f, 1f);
                gunArm.localScale = new Vector3(-1f, -1f, 1f);
            }
            else
            {
                transform.localScale = Vector3.one;
                gunArm.localScale = Vector3.one;
            }

            //rotate gun arm
            Vector2 offset = new Vector2(mousePos.x - screenPoint.x, mousePos.y - screenPoint.y);
            float angle = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg;
            gunArm.rotation = Quaternion.Euler(0, 0, angle);
        }
1 Answers

There are several ways to go about FPS controls in Unity but a common approach is to first lock the cursor (so it doesn't go everywhere), set it to invisible, and treat it as another axis.

The first parts will involve the following Cursor APIs. Also, make sure you disable and reenable these as the player enters and exists UI vs the FPS parts of your game!:

Cursor.lockState = CursosLockMode.Locked;
Cursos.visible = false;

The second part is simply another usage of Input.GetAxis. As the documentation for that API mentions, you can use "Mouse X" and "Mouse Y" as axis:

Input.GetAxis("Mouse X");
Input.GetAxis("Mouse Y");

That doc also mentions the following, in case those exact strings aren't working for you (but AFAIK, those two are the defaults when making a new unity project):

To set up your input or view the options for axisName, go to Edit > Project Settings > Input Manager. This brings up the Input Manager. Expand Axis to see the list of your current inputs. You can use one of these as the axisName. To rename the input or change the positive button etc., expand one of the options, and change the name in the Name field or Positive Button field. Also, change the Type to Joystick Axis. To add a new input, add 1 to the number in the Size field.

Related