Temp Id
Temp Id

Reputation: 387

How to get the direction in which the camera is facing in Unity3D?

I need to know the direction in which the camera is facing and move forward in that direction. Also I need to know to know the perpendicular horizontal and vertical directions with respect to the above direction. How to do that?

Also by default when I move my camera in only z-direction (starting from (0,0,0) and facing in z-direction), I create new instances of prefabs in a range at positions between some -x to +x and -y to +y, independent of each other. Now how will I determine where to create the instances depending on the new direction of camera?

Upvotes: 7

Views: 30594

Answers (1)

Heisenbug
Heisenbug

Reputation: 39204

I need to know the direction in which the camera is facing

Camera is simply a Component attached to a GameObject. It's orientated using the relative Transform.

Main Camera forward direction in world space can be accessed this way:

Camera.main.transform.forward;

The orthogonal basis vector right and up could be found in the same way:

Camera.main.transform.up;
Camera.main.transform.right;

Upvotes: 14

Related Questions