- Home /
Top-Down Mouse aiming help
To give context, the way it is currently setup, I can aim perfectly with a joystick however when I attempt to use the position of the mouse with the input system, it functions incorrectly.
public void OnAim(InputAction.CallbackContext context)
{
aimInput = context.ReadValue<Vector2>();
}
private void Aim(){
faceDirection = Vector3.forward * aimInput.x + Vector3.left * aimInput.y;
var desiredRotation = Quaternion.LookRotation(faceDirection);
transform.rotation = Quaternion.RotateTowards(transform.rotation,
desiredRotation, lookSpeed * Time.deltaTime);
}
Trying to figure out how to make this work with both controller & keyboard + mouse. Thank you for your help!
Joystick returns a direction vector. Mouse returns you a screen position vector. There are not the same, by definition.
Mouse position delta is a direction vector but needs to be either processed or formatted to be useful for pawn controllers.
Also, you can convert mouse pointer position to direction vector by comparing mouse and pawn screen positions (subtract them, apply some math to convert int
pixel counts to float
in -1_1 range, flip y).
Answer by NonagonKev · Feb 17 at 05:19 PM
Brackey's made a video on how to create a top down shooter with a mouse controller. You could create a bool to switch between controller and KBM as a test.
The timestamp will take you straight to where the finished code is for the mouse movement. https://youtu.be/LNLVOjbrQj4?t=569
Hope this helps!