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);
}
}