- Home /
How can I change the "forward" direction of my model?
in the car tutorial how can i change the forward vector for the car ? the complete scene setup the forward is pointing 45' degree to the track street i want the forward for the car to point always to the front of the car
this.transform.LookAt(this.transform.forward);
If i used the above code into the tutorial car it always look at 45' degree it doesnt look straight to the track road.
Answer by duck · Oct 23, 2010 at 12:53 PM
Sounds like your car has been modelled in your 3d modelling app with its origin (pivot) pointing in the wrong direction.
This is best fixed in the 3d app itself, although you can work around it by parenting your car model to an empty 3d game object. Then you can treat the empty game object as if it were the car (i.e. place scripts on it), and have the incorrectly aligned car model as a child, rotated to face the correct way.
i'm using the car from the tutorial itself the red one, the global forward for the car is 45' ! why is it like this ? the local is pointing toward the street but when i use the code above its not pointing toward the street !
Answer by KSP_Bro_AE · Oct 31, 2018 at 07:49 AM
After the transform.LookAt, you shut put: transform.Rotate(new Vector3(/*experiment here until it works*/));
No redoing 3D model, no parenting to empty, just a line of code.
Answer by Cornelis-de-Jager · Mar 06, 2019 at 09:09 PM
How i tend to get around this fairly easily is by putting my model in its own gameObject. I then child that game object to another transform which contains the movement scripts. Meaning I can rotate and change the position of my model any time without effecting the movement direction.
Answer by highpockets · Mar 06, 2019 at 11:32 PM
LookAt takes a transform or position as an argument, not a direction. The direction vector passed with transform.forward is going to be the normalized direction of wherever the cars z axis is pointing, but the cars z axis will lookat the world origin (0,0,0) + the normalized direction vector which may be 1,0,0 for example. So it will look at 1,0,0 in world space.. The proper use of LookAt in this case would be: LookAt(carsPosition + carTransform.forward) .. That is assuming that the car model’s z axis points toward the front of the car, if it’s not pointing to the front of the car than choose the direction that is (the red right direction vector for example) and use that instead..
Answer by hal0zer0 · Mar 06, 2019 at 08:49 PM
Neither of these responses was appropriate for my situation (which involved an auto-follow camera following from the wrong side) - I ended up just sticking a cube inside my backward model (optionally disabling the mesh renderer) and making THAT the target for my camera