- Home /
How to get the constant Y rotation of the camera
Question.... So I am trying to figure out how to change my movement controls depending on the Y rotation of the Camera.
This would be the code for just one of the 4 sides, but it seams to only check the Cameras Y rot at the start of the program instead of always checking it. I can not seam to put Camera.mainCamera.transform.rotation.y in a var.
This is in my Update function on the Prefab I am controlling.
if ((Camera.mainCamera.transform.rotation.y < 137) && (Camera.mainCamera.transform.rotation.y > 51))
{
//Front Between 137-50 camera rot.y GP Movement
if ((transform.position.z > 388.5) && (Input.GetKeyDown (KeyCode.D))) transform.Translate (Vector3(0,0,-33.5));
if ((transform.position.z < 392.5) && (Input.GetKeyDown (KeyCode.A))) transform.Translate (Vector3(0,0,33.5));
if ((transform.position.x > -84) && (Input.GetKeyDown (KeyCode.S))) transform.Translate (Vector3(-33.5,0,0));
if ((transform.position.x < 51) && (Input.GetKeyDown (KeyCode.W))) transform.Translate (Vector3(33.5,0,0));
}
Answer by Khada · Aug 23, 2012 at 09:47 AM
This should work fine:
float fYRot = Camera.main.transform.eulerAngles.y;
If your code is only running once at the start, are you sure it's in an update loop and not in a Start / Awake method?
That is C# right not javascript? in javascript its "var fyRot : float = Camera.mainCamera.transform.rotation.y" right? When I do that it says can not put camera.maincamera.transform.rotation.y in a var. And yes it is in the update function for sure. To me my code should check if Camera rot Y is <137 And >50 and if it is the controls should work if not then nothing happens. but that is not happening. It will only let me use the movement keys if the Camera.mainCameras Y rotation is already betwen the two HardcodeVars at the start of the game.
Yes, it's C# (I don't use java). Is it really "Camera.mainCamera" in java (rather than "Camera.main" in c#). Seems odd to have a different na$$anonymous$$g convention. You're using an API with debugging capabilities?
OH! I just realised you're using "transform.rotation.y"! Don't do that, use "transform.eulerAngles.y"
You rock $$anonymous$$hada! All I needed was the eulerAngles.y ins$$anonymous$$d of rotation.y
Your answer
Follow this Question
Related Questions
Push object in direction of camera 0 Answers
LookAt only when camera position changes 2 Answers
Object doesn't stick on Camera right Corner?? (Basic Question) 1 Answer
How do I move the camera to another object in the scene on mouse down? 1 Answer
Camera Orbiting Character: How to make an object move with another with no rotation 1 Answer