Fix rotation skipping
I've been working on VR project (testing on an oculus headset) and have come to a small issue with turning.
My camera is the child of an object called the XRLimiter which is supposed to rotate in the opposite direction of the camera whenever the camera rotates a certain number of degrees in either direction, relative to the player. The purpose of which is the VR Development old goalof not letting the character turn around and see the models face.
I've been able to get this to work almost perfectly, sole issue I have now is the player's rotation jumps suddenly when the VR user turns completely around, in other words when the difference of the camera and the XRLimiter's rotation reaches 180 degrees.
public void XRComplexTurning()
{
//Set local euler angles for all included objects to Vector3 for quick access
//The angles of the player
Vector3 playAngles = transform.localEulerAngles;
//The angles of the camera, and therefore the headset
Vector3 camAngles = camCon.gameCam.transform.localEulerAngles;
//The angles of the object XRLimiter to which the camera is parented
//This object will be used to offset the camera
//This object is in turn a descendent of the player, though not a direct child
Vector3 limiterAngles = camCon.xRLimiter.transform.localEulerAngles;
//Convert 0<>360 euler rotation to -180<>180 rotations
camFloat = camAngles.y;
if (camFloat > 180)
{ camFloat -= 360; }
limiterFloat = limiterAngles.y;
if (limiterFloat > 180)
{ limiterFloat -= 360; }
playFloat = playAngles.y;
if (playFloat > 180)
{ playFloat -= 360; }
//Find how far the camera has turned in relation to the player's forward facing direction
headsetFloat = camFloat + limiterFloat;
//Create float to hold threshhold overflow
//Currently created outside of function so it can be seen in the editor
overYFloat = 0;
//Create flow to use for multipliers based on turning left or right
//No longer used for actual calculation, but I keep it to help visualise things
float turnDir = 0;
//If looking too far right
if (headsetFloat > xRThreshhold)
{
//Set Y axis rotation adjustment
overYFloat = headsetFloat - xRThreshhold;
//Visualisation help
turnDir = 1;
print("Right");
}
//If looking too far left
else if (headsetFloat < -xRThreshhold)
{
//Set Y axis rotation adjustment
overYFloat = headsetFloat + xRThreshhold;
//Visualisation help
turnDir = -1;
print("Left");
}
//Rotate player character the same amount as the camera has turned passed the threshhold
transform.localEulerAngles = new Vector3(playAngles.x, playFloat + overYFloat, playAngles.z);
//Turn the XRlimiter the reverse direction to offset the camera and keep it within a cone compared to the player
camCon.xRLimiter.transform.localEulerAngles = new Vector3(limiterAngles.x, limiterFloat - overYFloat, limiterAngles.z);
}
I already know that the size of the skipping grows with the xRThreshold float, but I don't want to remove this free look area just to resolve this. I also already know not to adjust the camera directly, if I could there would be no need for the XRLimiter.
xRThreshold is set to 20 during testing.
Your answer
Follow this Question
Related Questions
Roll a ball, treating where the camera is rotating as forward? 0 Answers
c# - error CS0103: The name `hit' does not exist in the current context (cardboard switching) 1 Answer
Free Look Camera Rig Changing Rotation 1 Answer
my camera is rotating on Z axis when i just set it on X and Y 0 Answers
Navigation in VR Mode?!! 0 Answers