- Home /
Question by
highpockets · Jun 21, 2013 at 12:36 AM ·
lerpdirectionvector2
Finding the direction an object is moving and giving it a destination
I have an object that is lerping towards a vector2 and I want to find out the direction it is travelling and then pass it a new vector2 target to lerp to that is the same direction of travel, but just a shorter distance.
Any help would be much appreciated. Thanks in advance
Comment
Best Answer
Answer by robertbu · Jun 21, 2013 at 12:52 AM
You can use Vector2.Lerp():
var newDest = Vector2.Lerp(currentPosition, oldDest, 0.5);
This will find a position half way between the current position and old destination. You can calculate it by hand as well:
var newDest = currentPosition + (oldDest - currentPosition) * 0.5;
If you need it a specific distance from the current position, you can do:
var newDest = currentPosition + (oldDest - currentPosition).normalized * distance;
If the distance is shorter or equal to the destination, and you want distance, you can do:
var newDest = Vector2.MoveTowards(currentPosition, oldDest, distance);
The specific distance version was exactly what I needed. Thanks!