- Home /
dash towards mouse position? (New Input System)
I am making a multiplayer game, where a main mechanic is the ability to dash towards the mouse. After quite a bit of pain, I have worked out all of the inputs. Now here's the issue: I don't know how to actually make the player dash. I have the button to start the dash, the timer for the dash, and the mouse/controller input set up. I know how to use things like "rb.velocity" or "rb.AddForce" But, I don't know how to take my input's Vector2 and actually apply it to the dash ability, to control the direction. Does anyone know how I might do this? Here's the code I currently have:
public void Dash(InputAction.CallbackContext context)
{
// dash when dash button is pressed (dash button and the timer are handled in another void)
if (startDash == true)
{
Vector2 DashAim = context.ReadValue<Vector2>();
//dash
startDash = false;
}
}
Answer by homer_3 · Sep 01, 2021 at 03:17 PM
Is your game 3D? If so, do camera.screenpointtoray on dashaim, where camera is your main camera. Use a physics raycast on that result to cast a ray into your scene. The hit result's point will give you the point you want to dash towards. You can do (point - player position).normalized to get the direction you want to dash in.
If it's a 2D game, you can just do (dashaim - player position).normalized to get the direction.
I have it set to normalize the Vector2 inside of the input manager, so I won't need to do that in code. I'm just double checking to make sure I properly understand. It is 2D. I had written a little bit of code but checking it in the IDE i'm using showed it to have several errors. could you maybe give a code example of how I would do this in the context of what I have already?
Having the input manager normalize the returned mouse position is not a good idea. You need to have the player position and the mouse position in the same coordinate system. Of course, you could convert the player position to a normalized value, by why add that extra work in to begin with? You should have the input manager return the raw screen position of the mouse.
I don't really use Unity 2D, so I would've thought your player position would be a 2D vector, but if it's still a 3D vector, then do
Vector3 dashaim3 = dashaim;
Vector3 dashDir3 = (dashaim3 - camera.WorldToScreenPoint(player.transform.position)).normalized;
Vector2 dashDir = new Vector2(dashDir3.x, dashDir3.y);
Then you can use dashDir as the direction to dash in.
ok, i'm going to try this:
public void Dash(InputAction.CallbackContext context)
{
// dash when dash button is pressed
if (startDash == true)
{
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);
//dash
rb.AddForce(DashDir * DashSpeed);
cantAirJump = true;
startDash = false;
}
}
I have a physics material2D with 0 friction applied to the player, so it’s not that. The problem happens if I try to dash in midair as well. Although I think it’s because of the way I’m handling movement. In update I am constantly setting the velocity to input multiplied by move speed. I am going to change it so that that is only running if there is input.
I will also make sure that it is disabled during the dash, so that it won’t interfere if dashing while holding the movement keys. Thank you for all the help!
Answer by WI_ZzeR · Sep 01, 2021 at 03:34 PM
when you use rb.AddForce it asks you for a direction vector this direction is basically the vector going from your character to the mouse simply get the mouse global position and substract it to your player's position this will give you thatdirection vector, in case of a 2d game it will return a 2d vector, if not just add 0 as the Vector3's Z value. then you use AddForce as you know it
alright. after messing with some stuff I got this:
Vector2 posInScreen = Camera.main.WorldToScreenPoint(transform.position);
Vector2 DashAim = context.ReadValue();
//dash
rb.AddForce(DashAim - posInScreen * DashSpeed);
this isn't giving me any errors, but i'm going to check it in the project and see if it is working as expected.
this does not work at all. it's giving me some really weird behavior.
here's a video clip:
the video clip is using the last bit of code I sent.
Sorry I live far away from the United states and it was around 1am already, seems taht you figured out how to do it but to answer your question i think your variable DashAim is the issue here, what exactly does it do? rb.AddForce(posInScreen - player.transform.position * DashSpeed); your addforce should look like this.
it's alright, we got it sorted out. thanks though!
Your answer
![](https://koobas.hobune.stream/wayback/20220613042429im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I use controller knob input like mouse position? (New Input System) 1 Answer
How do I get the Vector2 from an input action in code? (New Input System) 1 Answer
Can I fake the mouse/touch position relative to the real mouse/touch position? 0 Answers
Force update of mouse position with the new input system 3 Answers