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;

        }

    }

2 Answers

2

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?

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!

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.

@WI_ZzeR are you still here?