- Home /
Face object towards a "direction" Crazy Taxi style
Hey everyone,
I have a compass needle that I've placed on a separate camera/layer so that it appears as a HUD object.
What I want is for this needle to face the direction that the gravity is set to.
When the player rotates the main camera, the needle should also rotate and point towards the direction of gravity at all times.
I'd also like to know how to do this to, say, the North direction (when the player looks down, the needle should also rotate).
I've got this, but it still doesn't respect the camera's rotation, only the gravity:
transform.forward = -Physics.gravity;
Thanks!
Answer by Cains · Nov 10, 2013 at 05:32 AM
As I understand it your needle is in a camera separate from your main camera, and therefore by default it's not affected by your main camera at all because it's completely separate.
What you want to do is in the Update() function of the second camera with the needle, orbit the camera around the needle by the transform.rotation of the main camera. If you don't understand what I mean I can probably whip something up really quick to show you.
If you wanted to do this for North instead of gravity you'd just set the transform.forward of the needle to the direction you want to be North. For example
transform.forward = new Vector3(1,0,0);
would point the needle in the positive x direction if you wanted that to be North.
Thanks for your response.
If I understand correctly, using pseudocode, it would look something like:
cam2.orbit(cam1.transform.rotation);
How would you orbit a camera around an object?
Also, rather than actually have the camera orbit the object, is there a way for the camera to remain static, but have the compass needle itself rotate based on the first camera's rotation? That was the answer I was looking for.
So effectively it would rotate based on cam1. So:
compass.transform.forward = cam1.transform.forward; compass.transform.rotation += gravityOffset; // Or, offset the makes it point north.
Thanks again!