- Home /
Transform look at rotates object wierdly
Hi, Im creating space simulator and I have problem with mouse based movement system. My goal is to create movement system similar to BattleFront 2 or WarThunder etc. I figured out that to do so, I can use ray from main camera with lookAt function. But for some strange reason, whenever I start the game, it rotates the ship about 90 degrees and it ,, looks at ,, the point of ray by its wing and not front. Do I miss something ?
Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector3 pointToLook = cameraRay.GetPoint(100F);
Debug.DrawLine(cameraRay.origin, pointToLook, Color.cyan);
transform.LookAt(pointToLook);
If you have any idea or link for proper tutorial to solve this, please help me :) Thank
Answer by unity_ek98vnTRplGj8Q · Mar 23, 2020 at 03:51 PM
If you look at your model you will see that the ship is facing along the red vector arrow, however in Unity this is the "Right" vector. You want your model to face along the blue vector arrow, which is the "Forward" arrow. If you rotate your model so that forward along the model of the ship corresponds to the blue arrow, your code will work correctly. If for some reason this is the way the model is imported, then the easiest fix is to make the model a child of an empty game object then rotate it 90 degrees.
If you don't want to change your model (which is what I recommend because an incorrectly oriented model will cause you pain down the road), you can simply add a 90 degree rotation after your transform.lookAt call so that it faces in the correct direction