Unity - Problems with Bone Rotations within constraints
I have a 3D character rigged model with animations that I have imported into Unity 5.1.3.
The character's body parts are listed as separate game objects which you can see in the Hierarchy panel. I have assigned these body parts as children to their corresponding bones. The idea is for the user to be able to rotate the body parts within rotational constraints; the user can select a body part, rotate it to his/her requirements, and then confirm the rotation.
But what happens is that, when the user clicks on the body part the 2nd time, it most of the time automatically rotates (although within the constraints set initially). Can anyone please kindly explain why this would be happening?
Here is some code that I have added:
void LateUpdate(){
if(selected_bone.name.Contains("required bone's name")){
rotX = ClampAngle(rotX, bone_rotX_min_limit, bone_rotX_max_limit);
rotY = ...
rotZ = ...
}
Quaternion newRot = Quaternion.Euler(rotX, rotY, rotZ);
selected_bone.localRotation = newRot;
}
float ClampAngle(float angle, float min, float max){
if(angle < -360){
angle += 360;
}
if(angle > 360){
angle -= 360;
}
return Mathf.Clamp(angle, min, max);
}
Comment