Gameobjects transform position minus range
I wasn't exactly sure what to call this question but here is the scenario
I have an enemy that is supposed to move towards my player. Now i want to find the players location minus a distance so that my enemy doesn't go directly up to my players face. However, i have been unable to find anything that allows me to do this.
I know that the Vector3
of my player is gameobject.transform.position
but how do I minus say a distance of 2
from that?
Answer by Hellium · Oct 28, 2017 at 09:03 PM
If I understand correctly, you want to find the point (called P) between an origin point (called O) and the target point (T) so that Distance(T,P) = radius
?
If so, here is how to compute it :
float radius = 2 ;
Vector3 direction = (originTransform.position - targetTransform.position ).normalized ;
Vector3 finalPosition = targetTransform.position + direction * radius ;
This code has a funny side effect it teleports my Player (target) to the enemy (gameobject)
You did not indicated you wanted a smooth movement. You asked a position, I gave it ;) You will find several questions about "How to get smooth movement to a position"
Your answer
Follow this Question
Related Questions
Are I stupid? Vector3 1 Answer
Vector 3 moving to certain place? 2 Answers
Applying direction into transform position 0 Answers
Helicopter Move Local Position,Local Position 1 Answer
How to properly make an object follow the edge of another object? 0 Answers