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?
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).
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.
playerCamera = GameObject. Find("MainCamera"). GetComponent<Camera>(); }
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With