Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 278
Temp Id Avatar asked Aug 13 '13 15:08

Temp Id


People also ask

How do you find direction facing in unity?

Facing direction of your player can be found by looking at the transform. rotation and as you seem to only be looking left and right you would use transform. rotation. y to check the y axis of your player (assuming the script is attached to your player).

How do you make a player face camera direction?

If you want the player to face the same direction as the camera, you need to copy the Z rotation of the camera into the player controller. So, in your player, you have a reference to the camera, get the forward vector and split it so you access only the Z.

How do you reference the main camera in unity?

playerCamera = GameObject. Find("MainCamera"). GetComponent<Camera>(); }


1 Answers

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;
like image 66
Heisenbug Avatar answered Sep 29 '22 08:09

Heisenbug