# Get specific distance between two Vector3 ?

Hi, so I am trying to get a Vector3 position EXACTLY “15” units way from current position.

attackRange = 15;
MouseLocation = Vector3 point where the mouse is;

I am trying this:

``````float dist = Vector3.Distance(transform.position, MouseLocation);
if (dist > attackRange) {
Vector3 correctPosition = blah;
}
``````

How can I get the exactly Vector3 position trying to head towards MouseLocation but can only move attackRange (15) units away?

Any info would be helpful, thanks.

This should work:`Vector3 correctPosition = Vector3.MoveTowards(transform.position, MouseLocation, attackRange);`

1 Like

Will be something like

``````transform.position+(MouseLocation-transform.position).normalized*15
``````

Love both those replies, thank you!
However, what if the length between player and mouse position is below attackRange (15), how do I get the 15 units away in that exact direction?
Thanks

The MoveTowards solution I posted handles this case automatically.

I’m about to start testing it now, but are you sure?
I thought that parameter was only for MAX distance and no more.
Not minimum?
Testing right now though!

edit: Yes so it handles if the distance is greater than 15 perfectly, however it will not if the distance is shorter than 15.

Can you explain what you want to happen if the distance is shorter than 15? Maybe draw another picture? It is not clear from your description above. If you want to just always get the point 15 units away exactly, you can use giollord’s solution.

1 Like

Yes, sorry for my lack of communication skills haha.
So here is an example of what I am trying to ask:

Even though my mouse position is less than “attackRange” 15, I want to make it a straight line from me to mouse position with EXACT length of 15 (attackRange).
Not 4.

Doesn’t the code @giollord posted already handle that? If you normalise the vector from the starting point to the mouse, it doesn’t matter how far away the mouse is before you multiply it to get the distance you want.

Alternatively, if you already have a vector pointing towards your cursor (say, to make your character point to your mouse), then normalising that vector, multiplying it, then adding it to your current position does the job as well.

1 Like