so i’m trying to make a dash in a top down 2D adventure game. I have the function where when I press LeftShift the player calls for a movetowards function and “dashes” towards where the mouse is.
What my problem now is I dont want the player to move passed a certain radius around the player (so the player can dash to a set distance or inside the radius but never outside)
If anyone has any idea on how to work on this to make it possible please let me know I would appreciate it very much!
Is your dash happening instantly or over the course of multiple frames?
If it’s instant (a single frame), you can just take whatever your third parameter to MoveTowards is and clamp it to your desired distance like: Mathf.Min(maxAllowedDashDistance, yourNormalThirdParameter)
If it’s over several frames, when you decide the position to dash towards, you can clamp the distance using the MoveTowards function as follows:
wait i’m kind of confused by this code. I am having the dash be a smooth transition so not over one frame its multiple. Looking at the code you provided it looks like the mathf.min will allow me to set a minimun distance? the code I have right now is this
if (Input.GetKeyDown(KeyCode.LeftShift) && canDash == true)
{
targetPosition = new Vector3(mousepos.x, mousepos.y, 0);
dashPressed = true;
canDash = false;
}
//calls for dash movement code
if(dashPressed == true)
{
PlayerDash();
}
the target position is the mouse position on the screen. So am I able to make the player dash in a set direction towards the mouse but still make it so it doesnt go outside of a set distance?
Ok so we’re talking about the second case then. All you need to do is change your “targetPosition” calculation slightly:
if (Input.GetKeyDown(KeyCode.LeftShift) && canDash == true)
{
targetPosition = new Vector3(mousepos.x, mousepos.y, 0);
// Limit to a certain distance away from the player
targetPosition = Vector3.MoveTowards(transform.position, targetPosition, maxDashDistance);
dashPressed = true;
canDash = false;
}
Obviously you’ll need to introduce a “maxDashDistance” float variable to achieve this. All this is doing is changing your target position to a point that is a maximum of maxDashDistance away from the player, but still in the direction of your mouse.
If I’m correct, you just need to modify your targetPosition calculation with exactly the code I just shared. Just adding that one new line. No other changes should be required.
I know it’s a little confusing using the MoveTowards function here for a different purpose than actually moving the player, but this will do what you want.
It’s only a speed in the context of movement over time.
Like I said, I know it’s a bit confusing but we’re using the same MoveTowards function here in a different way than in the actual movement code. Here we’re using it to get a point in the direction of your mouse position but a maximum distance away from your player.
MoveTowards does exactly what you’re looking for here. From the documentation, this is what it does:
So in this case, the two points are your player position and mouse position. So it will give us a point in the direction of your mouse but no further away than the max distance parameter. Try it out!
Ok so i’ve tried changing the last variable in the movetowards and its just the speed basically. I set it to one (not * time.deltatime) and it still goes as far as i click in any direction but at a slower speed
So I was able to fix it! i simply just made a new vector3.distance and did a check to see if the mouse position was shorter than the distance between the player and the mouse and if it returns true its able to do the dash