Hello men and women of the game making world, I’m currently making a small game with a character that can freely look around the world while the head rotates.
Variables
Quaternion bodyRotation;
Quaternion oldBodyRotation;
Vector3 totalRotation;
float xRotation = 0f;
float yRotation = 0f;
Code for the method to rotate camera
void RotateView()
{
float remainder; // body rotation - head rotation
bodyRotation = gameObject.transform.rotation;
// Mouse inputs
totalRotation.x -= Input.GetAxis("Mouse Y");
totalRotation.y += Input.GetAxis("Mouse X");
// Clamp vertical quaternion
totalRotation.x = Mathf.Clamp(totalRotation.x, -70f, 70f);
mainCamera.transform.rotation = Quaternion.Euler(totalRotation.x, totalRotation.y, 0f);
playerhead.transform.rotation = Quaternion.Euler(totalRotation.x, totalRotation.y, 0f);
remainder = bodyRotation.y - totalRotation.y;
// If head is x amount away from body rotate body
if (remainder > bodyRotation.y + 60 || remainder < bodyRotation.y - 60)
{
// If head is too far right rotate body right with head
if (totalRotation.y >= bodyRotation.y - 60)
{
bodyRotation.y -= remainder + 60;
}
else if (totalRotation.y <= bodyRotation.y + 60)
{
bodyRotation.y -= remainder - 60;
}
// Rotate body with given y rotation
gameObject.transform.rotation = Quaternion.Euler(0f, bodyRotation.y, 0f);
}
oldBodyRotation = bodyRotation;
}
Info
The end goal of the characters look script is to allow the character to look 60 degrees to the left or right of the character and if the number is exceeded then the body should rotate with the characters head. I have this all working well apart from when the characters head is over 60 degrees the body will constantly rotate even when turning the other direction.
The problem
The problem I’m having in other words is that once the head has reached 60 degrees the body will rotate with the head, however as I’m decreasing the head angle the body still rotates although the body should be still until I surpass 60 degrees away. From looking at the inspector once the head is passed 60 degrees it will stay 60 degrees until the body returns to 0,0 again as the body is trying to return to 0,0 while the head is limiting it. As it took me 2 days to figure out how to get where I am now I’m stumped on this problem. :)![]()
Video
https://www.youtube.com/watch?v=79YufJWPKug
Any input is much apricated!