Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Shoulders bone rotation broken

Discussion in 'Animation' started by elemarchi, Apr 13, 2023.

  1. elemarchi

    elemarchi

    Joined:
    Nov 16, 2022
    Posts:
    1
    hello, i was working on a new third person game so i tried IK for the first time
    and using blend tree
    my model is from mixamo.
    upload_2023-4-13_13-57-27.png


    the code is:
    Code (CSharp):
    1.     private void OnAnimatorIK(int layerIndex)
    2.     {
    3.         if (m_animator && m_cameralogic)
    4.         {
    5.             m_animator.SetBoneLocalRotation(HumanBodyBones.Neck, Quaternion.Euler(m_cameralogic.GetRotationX(), 0, 0));
    6.             m_animator.SetBoneLocalRotation(HumanBodyBones.LeftShoulder, Quaternion.Euler(m_cameralogic.GetRotationX(), 0, 0));
    7.             m_animator.SetBoneLocalRotation(HumanBodyBones.RightShoulder, Quaternion.Euler(m_cameralogic.GetRotationX(), 0, 0));
    8.         }
    9.     }
    10. }
    In playerLogic
    and:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraLogic : MonoBehaviour
    6. {
    7.  
    8.     Vector3 m_cameraTarget;
    9.     GameObject m_player;
    10.  
    11.     float m_cameraTargetOffset = 2.0f;
    12.     float m_distanceZ = 2.0f;
    13.  
    14.     float m_rotationX;
    15.     float m_rotationY;
    16.  
    17.     const float Min_x = -20.0f;
    18.     const float Max_x = 20.0f;
    19.  
    20.     const float Min_z = 2;
    21.     const float Max_z = 8;
    22.  
    23.  
    24.     // Start is called before the first frame update
    25.     void Start()
    26.     {
    27.         m_player = GameObject.FindGameObjectWithTag("Player");
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         m_cameraTarget = m_player.transform.position;
    34.         m_cameraTarget.y += m_cameraTargetOffset;
    35.  
    36.         if (Input.GetButton("Fire2"))
    37.         {
    38.             m_rotationY += Input.GetAxis("Mouse X");
    39.             m_rotationX -= Input.GetAxis("Mouse Y");
    40.             m_rotationX = Mathf.Clamp(m_rotationX, Min_x, Max_x);
    41.         }
    42.        
    43.         m_distanceZ -= Input.GetAxis("Mouse ScrollWheel");
    44.         m_distanceZ = Mathf.Clamp(m_distanceZ, Min_z, Max_z);
    45.     }
    46.  
    47.     void LateUpdate()
    48.     {
    49.         Quaternion CameraRotation = Quaternion.Euler(m_rotationX, m_rotationY, 0);
    50.         Vector3 cameraOffset = new Vector3(0, 0, -m_distanceZ);
    51.         transform.position = m_cameraTarget + CameraRotation * cameraOffset;
    52.         transform.LookAt(m_cameraTarget);
    53.     }
    54.  
    55.     public Vector3 GetForwardVector()
    56.     {
    57.         Quaternion rotation = Quaternion.Euler(0, m_rotationY, 0);
    58.         return rotation * Vector3.forward;
    59.     }
    60.  
    61.     public float GetRotationX()
    62.     {
    63.         return m_rotationX;
    64.     }
    65. }
    the neck is moving properly but the shoulders are broken
    plz help