- Home /
How do I use controller knob input like mouse position? (New Input System)
I recently made a dash ability that dashes towards the mouse, using the new input system. I want this ability to also work if using a controller, by reading the direction the knob is being pushed and using that for the direction of the dash. Does anyone know how I can set it up so that the direction the knob is being pressed works like the direction to the mouse from the player?
I had tried this setup:
but when dashing on the controller it would ignore the direction I was pressing the knob, and just dash downwards diagonally to the left, regardless of the mouse's position even. The part of the script I am using to read the input and dash looks like this:
public void Dash(InputAction.CallbackContext context)
{
Vector2 dashAim = context.ReadValue<Vector2>();
Vector3 dashAim3 = dashAim;
Vector3 dashDir3 = (dashAim3 - camera.WorldToScreenPoint(rb.transform.position)).normalized;
Vector2 dashDir = new Vector2(dashDir3.x, dashDir3.y);
//if button has been pressed
if (startDash == true)
{
//dash
rb.velocity = dashDir * dashSpeed;
cantAirJump = true;
startDash = false;
}
}
Answer by Hellium · Sep 03, 2021 at 05:47 PM
I have barely used the Input System, but from what I understand, the Position [Mouse]
entry will return the position of the mouse in the Screen Space ((0,0) → (Screen.width, Screen.height)) while the Left Stick [GamePad]
certainly returns values between (-1,-1) → (1,1), so Vector3 dashDir3 = (dashAim3 - camera.WorldToScreenPoint(rb.transform.position)).normalized;
does not make sense here.
I believe you will have to create two actions, a DashWithMouse and a DashWithStick action.
FOLLOWING CODE NOT TESTED
// To be called instead of the Dash function when adding the listener
public void ReadMouseInput(InputAction.CallbackContext context)
{
Vector2 mousePosition = context.ReadValue<Vector2>();
Vector3 direction = (mousePosition - camera.WorldToScreenPoint(rb.transform.position)).normalized;
Dash(new Vector2(direction.x, direction.y));
}
// Callback of the Left Stick
public void ReadStick(InputAction.CallbackContext context)
{
Vector2 stickDirection = context.ReadValue<Vector2>().normalized;
Dash(new Vector2(stickDirection.x, stickDirection.y));
}
public void Dash(Vector2 direction)
{
//if button has been pressed
if (startDash == true)
{
//dash
rb.velocity = direction * dashSpeed;
cantAirJump = true;
startDash = false;
}
}
Works like a charm! Sorry I was being so stubborn about changing my script, I guess I was too incompetent in this area to know whether I would have to or not. Thank you for helping me, in spite of me being a jerk.
That's ok, I am glad the code worked and I appreciate you acknowledged your faults. Good luck!
Your answer
Follow this Question
Related Questions
How do I use an Input axis instead of Input.mousePosition??? 1 Answer
How do I get the Vector2 from an input action in code? (New Input System) 1 Answer
dash towards mouse position? (New Input System) 2 Answers
How do I rotate an object using Vector2 inputs? (New Input System) 1 Answer
valve index A button mapped as grip 1 Answer