Clamping rotation doesn't seem to work

I have a script attached to my player’s head object which tells it to automatically look at the head of an enemy when you get close. The script works well in which it immediately turns to look at the enemy within a certain radius. But obviously, when I rotate the character’s body around, the head still stays in place and continues to look at the enemy at any angle, even when his body is facing the opposite direction.

I want to be able to lock the rotation so that his head can’t move no further than 25 degrees both left and right, up and down. I tried using euler angles, clamping, etc. None of these things work as it seems to just ignore the clamping and looks at the enemy from any angle regardless. I spent the past 24+ hours off and on looking up possible solutions in Google and none of them work either. Most of other people’s problems pertained to just controlling a turret of some sort, but in my particular case the movement is automatic.

        if(Vector3.Distance(target.position, myTransform.position) < turndistance && PLD_Knife.isAiming == false && PLD_Knife.isFiring == false)
        {
            isLookingAt = true;
            pHead.renderer.enabled = true;
            head.renderer.enabled = false;
            if(isLookingAt == true)
            {
                Vector3 lookPos = transform.position - target.position;
                lookPos.y = 0;
                transform.rotation = Quaternion.LookRotation(lookPos);
            }
        }

I don’t know what other code you have but… it looks like your “isLookingAt” will always be true once it goes on? In which case, it will always face and look at the lookPos anytime it’s within your turndistance.

Do you have some other “else” statement that shuts off your true flag if the rotation goes beyond your clamping?

Seems like you could just place an extra nested if-statement in there to check the clamping before you switch “isLookingAt” to true, and have an else to make sure it reverts to false.

 if(Vector3.Distance(target.position, myTransform.position) < turndistance && PLD_Knife.isAiming == false && PLD_Knife.isFiring == false)
        {
            if(within clamping parameters)
            {
                isLookingAt = true;
                pHead.renderer.enabled = true;
                head.renderer.enabled = false;
            }
            else
            {
                 isLookingAt = false;
                 <revert heads to normal>;
            }

            if(isLookingAt == true)
            {
                Vector3 lookPos = transform.position - target.position;
                lookPos.y = 0;
                transform.rotation = Quaternion.LookRotation(lookPos);
            }
        }

Though, you could clean that up a bit and eliminate some needless code. It seems like from that block that your isLookingAt variable is kind of unnecessary. A couple functions to switch the heads and revert them would probably be cleaner too.

You may have more code that would be helpful to the problem, but that’s all I can see from that block.

Instead of clamping, just add an angle check?

if(Vector3.Angle(lookPos, transform.forward) < 25)
    transform.rotation = Quaternion.LookRotation(lookPos);
1 Like