- Home /
Add a float to a Vector3 in a direction
Hi,
I'm trying to teleport my player to a distance in the direction of my mouse. I can check if my mouse is over a certain distance, and if so, then I want to teleport only to my maxDistance for my teleport (which is a float) in the Direction of my mouse. How do I know where to teleport too based on my current position, the mouse position and my float?
if(Vector3.Distance(transform.position,Camera.main.ScreenToWorldPoint (Input.mousePosition)) > _dashDistance)
{
// Teleport :D
}
Thanks!
Joey
Answer by Bunny83 · Dec 13, 2017 at 02:22 AM
In your case you can simply use "Vector3.ClampMagnitude". Something like this:
// vector from your current position to the mouse position
Vector3 dir = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position;
// clamp the "dir" vector to have a length between 0 and " _dashDistance".
dir = Vector3.ClampMagnitude(dir, _dashDistance);
ClampMagnitude is basically the same as doing:
if (dir.magnitude > _dashDistance)
dir = dir.normalized * _dashDistance;
Just more optimised. Normalizing a vector makes the vector length to be 1 unit. The vector will keep it's direction. So multiplying a normalized vector by a certain length will change the vector length to the specified value. The length / magnitude of a vector is its Euclidean distance.
I kind of understand why it should work, but it does not right now. When I click the button to do so, 2 things happen :
I get to the mouse cursor anyway, bypassing the maxDist.
OR
I go all the way back to somewhere very close to my inital Position?? I move all the way to somwhere far, I click the button and go back to the place I started the level ... (that is weird honnestly because I did not save my initial pos anywhere).
I did :
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F))
{
if(Vector3.Distance(transform.position,Camera.main.ScreenToWorldPoint (Input.mousePosition)) - 10 > _dashDistance)
{
Vector3 dir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
dir = Vector3.Clamp$$anonymous$$agnitude(dir, _dashDistance);
transform.position = dir;
}
else
{
transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.Translate(0, 0, 10);
}
}
Is your game in 2D or 3D? If 3D, Camera.main.ScreenToWorldPoint() needs a Vector3 where the z direction specifies the depth of the point away from the camera. Also why are you subtracting 10 from the distance check than translating by 10 from the z otherwise?
Look, if you take another look at your question at the top you haven't even mentioned how you move your object. So we can only make assumptions about your setup and how you use the code fragment you've posted.
"dir" is not a new worldspace position but it is the relative movement you should apply to your position. You don't need the if statement at all. You just do
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F))
{
Vector3 dir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
dir = Vector3.Clamp$$anonymous$$agnitude(dir, _dashDistance);
transform.position += dir; // note the "+="
}
$$anonymous$$eep in $$anonymous$$d that this will move your object instantaneously towards your mouse position but not more than _dashDistance from the current position.
If you always want to "dash" the same amount in the direction where the mouse position is you can just use
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F))
{
Vector3 dir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
transform.position += dir.normalized * _dashDistance;
}
Though i guess the first one is what you're actually after. So if the user clicks just 3 pixels from the current position he should only move those 3 pixels. But if he clicks further away than the allowed distance it should only move that distance.
Your answer
Follow this Question
Related Questions
Vector3(x,y,variable)? 0 Answers
Keep camera at certain distance from character when rotating 3 Answers
Return to Start Position 1 Answer
Make Navmesh agent follow closest object with tag 0 Answers
Check distance between many objects 3 Answers