# Need help with a MoveTowards function

Discussion in 'Scripting' started by Robster95, Aug 10, 2020.

1. ### Robster95

Joined:
Jul 3, 2019
Posts:
54
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!

2. ### PraetorBlue

Joined:
Dec 13, 2012
Posts:
3,291
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:
Code (CSharp):
1. Vector3 startingPosition;
2. Vector3 targetPosition;
3.
4. targetPosition = Vector3.MoveTowards(startingPosition, targetPosition, maxAllowedDashDistance);
Then continue with your code as normal.

Robster95 likes this.
3. ### Robster95

Joined:
Jul 3, 2019
Posts:
54

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
Code (csharp):
1.
2.  if (Input.GetKeyDown(KeyCode.LeftShift) && canDash == true)
3.         {
4.             targetPosition = new Vector3(mousepos.x, mousepos.y, 0);
5.             dashPressed = true;
6.             canDash = false;
7.         }
8.         //calls for dash movement code
9.         if(dashPressed == true)
10.         {
11.             PlayerDash();
12.         }
13.
and
Code (csharp):
1.
2. public void PlayerDash()
3.     {
4.         transform.position = Vector3.MoveTowards(transform.position, targetPosition, dashTime * Time.deltaTime);
5.         if (transform.position == targetPosition)
6.         {
7.             dashPressed = false;
8.             canDash = true;
9.         }
10.     }
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?

4. ### PraetorBlue

Joined:
Dec 13, 2012
Posts:
3,291
Ok so we're talking about the second case then. All you need to do is change your "targetPosition" calculation slightly:

Code (CSharp):
1.  if (Input.GetKeyDown(KeyCode.LeftShift) && canDash == true)
2.         {
3.             targetPosition = new Vector3(mousepos.x, mousepos.y, 0);
4.             // Limit to a certain distance away from the player
5.             targetPosition = Vector3.MoveTowards(transform.position, targetPosition, maxDashDistance);
6.             dashPressed = true;
7.             canDash = false;
8.         }
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.

5. ### Robster95

Joined:
Jul 3, 2019
Posts:
54
Ok and I will be able to make that all possible by using the methods you stated above?

6. ### PraetorBlue

Joined:
Dec 13, 2012
Posts:
3,291
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.

Robster95 likes this.
7. ### Robster95

Joined:
Jul 3, 2019
Posts:
54
ok and the new line will be the mathf.min correct?

8. ### PraetorBlue

Joined:
Dec 13, 2012
Posts:
3,291
No, this one:

Code (CSharp):
1.             targetPosition = Vector3.MoveTowards(transform.position, targetPosition, maxDashDistance);
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.

9. ### Robster95

Joined:
Jul 3, 2019
Posts:
54
I have it just like that but there's no parameter that stops the player from moving outside of a set radius

10. ### PraetorBlue

Joined:
Dec 13, 2012
Posts:
3,291
No, currently you just have this:
Code (CSharp):
1. targetPosition = new Vector3(mousepos.x, mousepos.y, 0);
I'm suggesting adding a line so it's like this:
Code (CSharp):
1. targetPosition = new Vector3(mousepos.x, mousepos.y, 0);
2. targetPosition = Vector3.MoveTowards(transform.position, targetPosition, maxDashDistance);
Now the "maxDashDistance" parameter can limit the distance away from the player for the end point of the dash.

11. ### Robster95

Joined:
Jul 3, 2019
Posts:
54
I thought the third parameter was the speed of how fast it would travel to the targetposition

12. ### PraetorBlue

Joined:
Dec 13, 2012
Posts:
3,291
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!

13. ### Robster95

Joined:
Jul 3, 2019
Posts:
54
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

14. ### Robster95

Joined:
Jul 3, 2019
Posts:
54
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

unityunity