- Home /
Headlight rotation issue when moving the camera
Hello,
In my game the first player controls the camera using the keyboard. He can make the camera look up and down as well as left and right. The second player uses a phone with a gyroscope to control a headlight. The headlight is a child of the camera, because it's supposed to be a vehicle and a vehicle's headlight always turns with the vehicle itself. Driving straight is not a problem, but as soon as the first player takes a turn, suddenly the gyroscope headlight controller stopps working correctly. Left and right seems to be fine at any time, but up and down gets reversed. Since the code that applies the rotations is scattered in multiple scripts, I will try to give you a short version.
This is the code that rotates the camera (first player):
if (Input.GetKey("up") || Input.GetKey("w"))
transform.Rotate(-speedControls * Time.deltaTime, 0f, 0f);
if (Input.GetKey("down") || Input.GetKey("s"))
transform.Rotate(speedControls * Time.deltaTime, 0f, 0f);
if (Input.GetKey("left") || Input.GetKey("a"))
transform.Rotate(0f, -speedControls * Time.deltaTime, 0f);
if (Input.GetKey("right") || Input.GetKey("d"))
transform.Rotate(0f, speedControls * Time.deltaTime, 0f);
This is the code related to the gyroscope (second player):
// set the headlight's rotation to the gyroscope's rotation
headlight.transform.rotation = phoneRotation;
// make sure that the headlight is rotating with the camera / vehicle
headlight.transform.Rotate(-cam.transform.localRotation.eulerAngles);
The first player's input will be handled first, then comes the gyroscope input. The gyroscope input entirely overwrites the first player's input, as seen in the first line. That's why I have added the second line, which is supposed to redo the first player's input.
Have you tried using world rotation ins$$anonymous$$d of local?.
Yes, I have tried that as well. The result is very confusing to me. Up and down seems to be not working at all anymore.