- Home /
Look for the player isn't working
I was watching this official tutorial on Unity: https://www.youtube.com/watch?v=uv2QSvuwIDs
I wish that instead of looking for the mouse he looked to the Player. So I put "public transform player;" and in FixedUpdate in place of "Input.mousePosition" I put "player.position". But it does not work.
The complete code is:
 public Transform player;
 
     void FixedUpdate(){
 
         var mousePosition = Camera.main.ScreenToWorldPoint(player.position);
         Quaternion rot = Quaternion.LookRotation(transform.position - mousePosition, Vector3.forward);
         transform.rotation = rot;
         transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z);
         rigidbody2D.angularVelocity = 0;
 
     }
I'm sorry for bad English. Help me please?
Answer by robertbu · Oct 14, 2014 at 03:25 AM
Input.mousePosition is in screen coordinates and must be converted to world coordinates. 'player' is already in world coordinates. So delete line 5, and replace 'mousePosition' by 'player.position'.
It worked! Thanks a lot man. I love you. Serious. But I had to change the line "transform.rotation = rot;" for "transform.rotation = Quaternion.Slerp (transform.rotation, rot, Time.deltaTime * 2);" to make a smooth rotation. Just leaving it here for someone someday need. Sorry for bad english and thank you.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                