Bind mouse with the new system input

Hi,

I want to use the mouse to control the ship, like we do with the keyboard or the gamepad. But I can not figure out how to bind it and use it from scripts. ¿Can somebody help me?

Best regards,

Hi,

Welcome to our community! :slight_smile:

The mouse exists in the Screen Space, your spaceship in the World Space. Look up the ScreenToWorldPoint method in the API. That’s what you need. Add Debug.Logs to your code to learn what values you are getting. That will help you find a solution.


See also:

1 Like

Hi thanks for the answer. But there is something like:

    float mouseX = Input.GetAxis("Mouse X");
    float mouseY = Input.GetAxis("Mouse Y");

in the new system?

Best regards,

Have you already taken a look at the manual? Looking for specific keywords could speed your research up.

https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/QuickStartGuide.html

https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Migration.html#unityengineinputmousepositionhttpsdocsunity3dcomscriptreferenceinput-mousepositionhtml

Here is another way to do the same thing. Look in Edit → Project Settings → Input Manager → Axes → Fire1 and Fire2

Take note that “Fire0” and “Fire1” correspond to the Mouse L & R buttons. There’s more to explore, but let’s stop here.

This code has been modified from the Delivery Driver script:

If you’ve worked through Delivery Driver, I think this will make sense to you. This script will allow the player to move fwd, bkwd, and strafe with w, a, s, and d. The player can also steer like a car with MouseL or MouseR.

I didn’t make this for the my GameDev project, but it is an offshoot of my Delivery Driver game so the code is very similar. I was working on it last night for something else, trying some things out.

Also note: This code is not complete for the purpose I have intended, so you might need to add a steering variable or multiply something to get your code to do what you want it to do. Just follow what the GameDev instructors show in the video.

The most important thing I wanted to show you here is:

  1. Where to find the keybindings
  2. How to use the information from the Input Manager Window in the code in a similar way to what you’ve already been shown with “Horizontal” and “Vertical”

This will make your mouse buttons work. I think you already figured out the rest.

I hope this is helpful.

1 Like

Hi thanks for your answers, but both methods are not using the new input system.

Ahh…new input system…didn’t catch that.

Hi,

I tried this solution, but seems that is using old input system:

  private Vector3 inMouse;

    void Update()
  {   
    inMouse = Input.mousePosition;
  }
  private uint TurretMovement()
  {
    Debug.Log($" mouse position: {inMouse}");
    return 0;
  }

This give me the next error:

when the project settings are already migrated to the new system:

Best regards,

Your old Input Manager is not connected, as you mentioned…it won’t be used of course.

I see the new Input System there below it in your pic. What’s in there?

I followed the steps in Space Assault lecture.

In the input system package I have:

And I already binded the keyboard with:

image

But I don’t know how succesfully bind the mouse

1 Like

I already found it, is a little tricky (at least for me…). Is very simple you only need to bind the action with:

image

and select mouse position
image

But, here come the trick, in the code, you should get the value as Vector2 type

image

Thanks a lot for your efforts.

1 Like

Coooool! :slightly_smiling_face:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms