- Home /
Get rotation based on closest 90 degrees to camera transform?
I'm trying to set a Vector3 rotation value based on camera orientation. I found 2 snippets of script that I think can help, but I'm not sure how to make them work in context. The first snippet sets the rotation directly based on the camera transform:
Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
targetDirection = Camera.main.transform.TransformDirection(targetDirection);
targetDirection.y = 0.0f;
The other rotates a game object to the nearest 90 degree axis:
Vector3 vec = transform.eulerAngles;
vec.x = Mathf.Round(vec.x / 90) * 90;
vec.y = Mathf.Round(vec.y / 90) * 90;
vec.z = Mathf.Round(vec.z / 90) * 90;
transform.eulerAngles = vec;
What I want is to get a Vector3 rotation value based on the 90 degree axis that the camera transformation rounds to. Any advice on this?
My first instinct was to do something like the following (using an empty placeholder game object as "coordSys")
Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
Vector3 vec = Camera.main.transform.eulerAngles;
vec.x = Mathf.Round(vec.x / 90) * 90;
vec.y = Mathf.Round(vec.y / 90) * 90;
vec.z = Mathf.Round(vec.z / 90) * 90;
coordSys.eulerAngles = new Vector3(x, y, z);
targetDirection = coordSys.transform.TransformDirection(targetDirection);
targetDirection.y = 0.0f;
This works for some camera angles, but other times it does not
Answer by Ramlock · Mar 26, 2018 at 05:43 AM
Is this what you want? I didn't really understand what you're trying to achieve.
Vector3 vec = Camera.main.transform.eulerAngles;
vec.x = Mathf.Round(vec.x / 90) * 90;
vec.y = Mathf.Round(vec.y / 90) * 90;
vec.z = Mathf.Round(vec.z / 90) * 90;
Camera.main.transform.eulerAngles = vec;
Answer by Malkalypse · Mar 26, 2018 at 03:44 PM
No, I am not trying to rotate the camera. I am trying to establish a coordinate system BASED on the camera but at a multiple of 90 degrees in world space.