Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

cam rotate with head during animation/mecanim

Discussion in 'Animation' started by Eve2015, Mar 23, 2015.

  1. Eve2015

    Eve2015

    Joined:
    Mar 9, 2015
    Posts:
    9
    Hi guy!;)

    I'm not sure if this is the right place or not and please feel free to move it to the correct section, anyway, can somebody please, please, and please help me how to rotate head with camera during mecanim animation? I've found this script but it moves the entire body and not the head only when rotating the camera.:(

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. // Very simple smooth mouselook modifier for the MainCamera in Unity
    5. // by Francis R. Griffiths-Keam - www.runningdimensions.com
    6.  
    7. [AddComponentMenu("Camera/Simple Smooth Mouse Look ")]
    8. public class MouseLook : MonoBehaviour
    9. {
    10.     Vector2 _mouseAbsolute;
    11.     Vector2 _smoothMouse;
    12.    
    13.     public Vector2 clampInDegrees = new Vector2(360, 180);
    14.     public bool lockCursor;
    15.     public Vector2 sensitivity = new Vector2(2, 2);
    16.     public Vector2 smoothing = new Vector2(3, 3);
    17.     public Vector2 targetDirection;
    18.     public Vector2 targetCharacterDirection;
    19.    
    20.     // Assign this if there's a parent object controlling motion, such as a Character Controller.
    21.     // Yaw rotation will affect this object instead of the camera if set.
    22.     public GameObject characterBody;
    23.  
    24.     void Start()
    25.     {
    26.         targetDirection = transform.localRotation.eulerAngles;
    27.        
    28.         // Set target direction for the character body to its inital state.
    29.         if (characterBody) targetCharacterDirection = characterBody.transform.localRotation.eulerAngles;
    30.     }
    31.    
    32.     void Update()
    33.     {
    34.         // Ensure the cursor is always locked when set
    35.         Screen.lockCursor = lockCursor;
    36.        
    37.         // Allow the script to clamp based on a desired target value.
    38.         var targetOrientation = Quaternion.Euler(targetDirection);
    39.         var targetCharacterOrientation = Quaternion.Euler(targetCharacterDirection);
    40.        
    41.         // Get raw mouse input for a cleaner reading on more sensitive mice.
    42.         var mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
    43.        
    44.         // Scale input against the sensitivity setting and multiply that against the smoothing value.
    45.         mouseDelta = Vector2.Scale(mouseDelta, new Vector2(sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));
    46.        
    47.         // Interpolate mouse movement over time to apply smoothing delta.
    48.         _smoothMouse.x = Mathf.Lerp(_smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
    49.         _smoothMouse.y = Mathf.Lerp(_smoothMouse.y, mouseDelta.y, 1f / smoothing.y);
    50.        
    51.         // Find the absolute mouse movement value from point zero.
    52.         _mouseAbsolute += _smoothMouse;
    53.        
    54.         // Clamp and apply the local x value first, so as not to be affected by world transforms.
    55.         if (clampInDegrees.x < 360)
    56.             _mouseAbsolute.x = Mathf.Clamp(_mouseAbsolute.x, -clampInDegrees.x * 0.5f, clampInDegrees.x * 0.5f);
    57.        
    58.         var xRotation = Quaternion.AngleAxis(-_mouseAbsolute.y, targetOrientation * Vector3.right);
    59.         transform.localRotation = xRotation;
    60.        
    61.         // Then clamp and apply the global y value.
    62.         if (clampInDegrees.y < 360)
    63.             _mouseAbsolute.y = Mathf.Clamp(_mouseAbsolute.y, -clampInDegrees.y * 0.5f, clampInDegrees.y * 0.5f);
    64.        
    65.         transform.localRotation *= targetOrientation;
    66.        
    67.         // If there's a character body that acts as a parent to the camera
    68.         if (characterBody)
    69.         {
    70.             var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, characterBody.transform.up);
    71.             characterBody.transform.localRotation = yRotation;
    72.             characterBody.transform.localRotation *= targetCharacterOrientation;
    73.         }
    74.         else
    75.         {
    76.             var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, transform.InverseTransformDirection(Vector3.up));
    77.             transform.localRotation *= yRotation;
    78.         }
    79.     }
    80.  
    81.  
    82. }