- Home /
 
Make object move after facing right direction
Hello. I've got another problem. I'll explain.
So I've got a bunch of objects. I tell them to move to a target location. Right now they start moving right after the click. How can I make them rotate to face the direction they're about to move before doing anything else?
I've tried writing like this. This, however, does not work.
     //squad[] is where I hold the objects
 
     if(squad[index].transform.rotation != Quaternion.LookRotation(direction)) {
         squad[index].transform.rotation = Quaternion.Slerp(squad[index].transform.rotation, Quaternion.LookRotation(direction), Time.deltaTime * 10);
     }
     else {
         squad[index].transform.position = Vector3.MoveTowards(squad[index].transform.position, target, Time.deltaTime * 2);                
     }
 
               Surely there's a better way to do this.
Answer by TonyLi · Dec 04, 2013 at 06:55 PM
Coroutines are a great way to code a sequence of actions. You can write a coroutine that loops Quaternion.Slerp() over a specified duration to rotate the object into the right direction, then loops MoveTowards() to translate it to the target.
Better yet, look into steering behaviors. It's a more flexible generalization, and you can do a lot more with it. UnitySteer is a popular, open source implementation.
Your answer