Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Input.GetAxis("Mouse X") or Input.GetAxis("Mouse Y") to the new Input System?

I'm new on Unity Scripting, and I'm trying to make a ThirdPersonCamera, so following this tutorial he can move the mouse up and down, and left and right correctly

enter image description here

The script used was

posY += Input.GetAxis("Mouse X") * mouseSensitivity;
posX -= Input.GetAxis("Mouse Y") * mouseSensitivity;
// mouseSensitivity can be changed

Vector3 targetRotation = new Vector3(posX, posY);
transform.eulerAngles = targetRotation;

Due to the new Input System, using Input.GetAxis("Mouse X") throws an InvalidOperationException: You are trying to read Input using the UnityEngine.Input class, but you have switched active Input handling to Input System package in Player Settings error.

So I tried to use

private PlayerInputMovement inputCamera;

void Awake(){
    inputCamera = new PlayerInputMovement();
    inputCamera.Player.Camera.performed += context => cameraInput = context.ReadValue<Vector2>();
}

void Update(){
    Camera();
}

void Camera(){ 
    posY += cameraInput.x * mouseSensitivity;
    posX -= cameraInput.y * mouseSensitivity;
    // mouseSensitivity can be changed

    Vector3 targetRotation = new Vector3(posX, posY);
    transform.eulerAngles = targetRotation;
}

And got this, but if I keep my mouse on an axis, it keeps rotating to that side

enter image description here

So... is this a correct way to replace the old Input.GetAxis("Mouse X") or Input.GetAxis("Mouse Y") to the new Input System? How can I stop it from keeping rotating?

like image 978
just_add_coffee Avatar asked Jan 20 '26 00:01

just_add_coffee


2 Answers

Mouse.current.delta.x.ReadValue()
Mouse.current.delta.y.ReadValue()
like image 160
shingo Avatar answered Jan 21 '26 12:01

shingo


You probably tried to import a new input system package for multiple input devices compatibility. These type of errors are due to conflict between old and new input system packages and are probably resolved in latest updates. To resolve this issue, Go to Edit -> Project Settings->Player->Under Other Settings under Configuration is the option Active Input Handling. Select Both. Unity will restart. Now your problem should be solved. You will be able to use old input system packages and the new ones also simultaneously. Cheers. P.S Click here

like image 41
Hashir Ali Avatar answered Jan 21 '26 13:01

Hashir Ali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!