- Home /
Move Obj A towards Obj B BUT keep going in same direction when Obj A has reached Obj B
The title of this question says it all. Basically I want the object to keep going in the same direction even when it's reached the target. I was trying to use Vector3.MoveTowards but as you know the Object will stop traveling when it has reached it's destination.
How can I do this?
Answer by sethuraj · May 05, 2015 at 04:12 AM
Instead of MoveTowards,Calculate the direction vector between Obj A and Obj B.Use that direction vector to translate.
//Get the direction vector between A and B
Vector3 Direction = ObjA_Pos - ObjB_Pos;
//Normalize it
Direction.Normalize();
//Translate along the direction
transform.translate(Direction*Time.deltaTime*Speed);
Incase your Obj B is not moving at all(Static) you just have to calculate the Direction vector only once.Else if your ObjB is a moving object,Update the Direction vector until ObjA reaches ObjB.Use Vector3.Distance()
float dist = Vector3.Distance(ObjA_Pos,ObjB_Pos);
if(dist>0.5f)
{
//Get direction vector
Direction = ObjA_Pos - ObjB_Pos;
//Normalize it
Direction.Normalize();
}
//Translate along the direction
transform.translate(Direction*Time.deltaTime*Speed);
Until ObjA reaches near ObjB the direction vector will be updated once its within the bound,the direction will not get updated but still ObjA will still move along the last direction.
Your answer
Follow this Question
Related Questions
Move from one position to another in x seconds 2 Answers
Prevent jittering of object movement 0 Answers
How to smooth movement object in one-axis? 1 Answer
Npc Movement 0 Answers
using MoveObject to perform 2 translations at the same time 1 Answer