Question by
francescoanf96 · Nov 19, 2020 at 04:36 PM ·
bones
Bone Rotate Only in it's local Axis
I'm trying to get Spine to look at the center of the screen, but when i rotate up the camera, the spine bone only rotate in it's local X axis. It seems to be a stupid question, because i use the "SetBoneLocalRotation" function, but.. how can i rotate it in the exact direction?
Vector3 CenterScreen = new Vector3(GameObject.FindWithTag("CrossHair").transform.position.x,GameObject.FindWithTag("CrossHair").transform.position.y,Camera.main.farClipPlane);
Vector3 point = Camera.main.ScreenToWorldPoint(CenterScreen);
Vector3 forward = (point - animator.GetBoneTransform(Spine).transform.position).normalized;
Vector3 up = Vector3.Cross(forward, transform.right);
Quaternion rotation = Quaternion.Inverse(transform.rotation) * Quaternion.LookRotation(forward, up);
animator.SetBoneLocalRotation(Spine, rotation);
immagine.jpg
(191.2 kB)
Comment
Answer by francescoanf96 · Nov 20, 2020 at 06:19 PM
I solved it by myself.... i post the code, maybe i can help someone...
void OnAnimatorIK()
{
if(animator) {
Vector3 CenterScreen = new Vector3(GameObject.FindWithTag("CrossHair").transform.position.x,GameObject.FindWithTag("CrossHair").transform.position.y,Camera.main.farClipPlane);
Vector3 point = Camera.main.ScreenToWorldPoint(CenterScreen);
Vector3 forward = (point - animator.GetBoneTransform(Spine).transform.position).normalized;
Vector3 up = Vector3.Cross(forward, transform.right);
Quaternion rotation = Quaternion.Inverse( animator.GetBoneTransform(Spine).transform.rotation) * Quaternion.LookRotation(forward, up);
animator.SetBoneLocalRotation(Spine, rotation*Quaternion.Euler(0,40,0));
}
}
The problem was in "QuaternionInverse" where I had put the main Parent rotation instead of animator.GetBoneTransform(Spine).transform.rotation. Also i add Quaternion.Euler(0,40,0) to correct the rest of the upper body which was slightly rotated in another direction than the spine, caused by the type of animation