Move characters body according to amount of head rotation away from the body

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. :):roll_eyes:

Video
https://www.youtube.com/watch?v=79YufJWPKug

Any input is much apricated!

You cannot use the .y field of a Quaternion. Same goes for the .x, .z and .w fields. Those are NOT euler angles. They are internal vector quantities that are completely useless to you.

You might be able to use the .eulerAngles.y property, but it is subject to gimbal lock and even before gimbal lock, it will always return between -180 and 180.

It’s always best to track your OWN float variable for heading and head-pointing, then drive the rotations directly. Here is such a discussion related to gun turrets:

Turret aiming/rotating:

https://discussions.unity.com/t/783469/2

1 Like

Thanks for the quick reply, I’ll have a look at the thread now. Although this might as well have been written in Spanish haha.

Okay, here it is another way:

If you think the 60 here correlates to degrees, that’s not correct. You are effectively comparing bananas and apples. How many bananas and apples are there in 60 degrees?

Don’t use those fields. They are not helpful to you.

The key point is you should ONLY create and put Quaternions into a rotation (or localRotation).

You should NEVER read Quaternions out of them unless you really know what you’re doing.

Instead, keep your own variables representing angles, and create Quaternions and put them INTO the rotation (or .localRotation).

Create Quaternions using Quaternion.Euler(xAngle, yAngle, zAngle);

1 Like

This clears things up loads, I’ve read into eulerAngles and quaternions and It’s helped to get a bigger understanding on how objects work in 3D. I would’ve totally over looked this doing my own research.

1 Like