- Home /
Creating a homing missile on 2D, problem with transform.lookat()
I'm trying to make a script for a homing missile, but the rotation when reaching 270 degrees seems to be messed up, my code (for testing purposes right now):
     void FixedUpdate () {
         transform.LookAt (trans.position); //face the target
         transform.rotation = new Quaternion(0,0,transform.rotation.z, transform.rotation.w); //x and y set to 0 since we want to rotate only the z axis
 }
But when the z axis reaches 270 degrees it just flips and faces the opposite direction, i dont know what else to try right now.
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by robertbu · Feb 16, 2014 at 01:19 AM
Construct your sprite so that 'forward' is the right side. Then you can do:
 Vector3 dir = trans.position - transform.position;
 float angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg;
 transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
or if you don't fancy mutilating your sprites, just add -90
float angle = ($$anonymous$$athf.Atan2(dir.y,dir.x) * $$anonymous$$athf.Rad2Deg) -90; 
Your answer
 
 
             Follow this Question
Related Questions
Homing missile not working. 1 Answer
Problem at 180 degrees when rotating slowly 1 Answer
Homing missile and LookAt issue 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                