Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Problem with head shaking

Discussion in 'Scripting' started by Mirace, Jun 24, 2014.

  1. Mirace

    Mirace

    Joined:
    Nov 29, 2010
    Posts:
    481
    Hi !

    Im currently bobbing camera on Y axle when player is moving . It works. But i also writed script to rotate camera @ Z axle while walking creating an illusion of steps. It works only if i disable mouse look script:

    Code (JavaScript):
    1.  
    2. @script AddComponentMenu ("Camera-Control/Mouse Look")
    3. enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    4. var axes = RotationAxes.MouseXAndY;
    5. var sensitivityX : float = 15;
    6. var sensitivityY : float = 15;
    7.  
    8. var minimumX : float = -360;
    9. var maximumX : float = 360;
    10.  
    11. var minimumY : float = -60;
    12. var maximumY : float = 60;
    13.  
    14. var rotationX : float = 0;
    15. var rotationY : float = 0;
    16.  
    17. private var originalRotation : Quaternion;
    18.  
    19. function Update () {
    20.  
    21.     if (axes == RotationAxes.MouseXAndY) {
    22.         rotationX += Input.GetAxis("Mouse X") * sensitivityX;
    23.         rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    24.  
    25.         rotationX = ClampAngle (rotationX, minimumX, maximumX);
    26.         rotationY = ClampAngle (rotationY, minimumY, maximumY);
    27.      
    28.         var xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
    29.         var yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
    30.      
    31.         transform.localRotation = originalRotation * xQuaternion * yQuaternion;
    32.     }
    33.     else if (axes == RotationAxes.MouseX) {
    34.         rotationX += Input.GetAxis("Mouse X") * sensitivityX;
    35.         rotationX = ClampAngle (rotationX, minimumX, maximumX);
    36.  
    37.         xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
    38.         transform.localRotation = originalRotation * xQuaternion;
    39.     }
    40.     else {
    41.         rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    42.         rotationY = ClampAngle (rotationY, minimumY, maximumY);
    43.  
    44.         yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
    45.         transform.localRotation = originalRotation * yQuaternion;
    46.     }
    47. }
    48.  
    49. function Start () {
    50.     if (rigidbody)
    51.         rigidbody.freezeRotation = true;
    52.     originalRotation = transform.localRotation;
    53. }
    54.  
    55. static function ClampAngle (angle : float, min : float, max : float) : float {
    56.     if (angle < -360.0)
    57.         angle += 360.0;
    58.     if (angle > 360.0)
    59.         angle -= 360.0;
    60.     return Mathf.Clamp (angle, min, max);
    61. }
    What causes this and how i fix it ?
     
  2. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    This script is probably overwriting the rotation for your headbob, asuming that you're using a rotation head bob, instead of a position head bob.
    ~
    Did you try using a additional parent empty gameobject to do the rotation, and use the headbob script on the camera itself?

    You can check out the first person controller in the Sample Assets(Beta), it also has a head bob script.
    Study it, and try to understand the logic behind it.
     
  3. Mirace

    Mirace

    Joined:
    Nov 29, 2010
    Posts:
    481
    Hi, and thank you for respond.

    I have heavily modded headbobber script which im using to handle bobbing. As i made clear earlier that mouse look script is behaving badly , problem is there , not in head bobber script.
     
  4. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Answer my question...
    that should fix your problem.
     
  5. Mirace

    Mirace

    Joined:
    Nov 29, 2010
    Posts:
    481
    Playerprefab is gameobject which has Camera gameobject as children. I use Mouse look script at Playerprefab and on Camera children object. Playerprefab has X axle and Camera object has Y axle. Camera object also has headbobber script attached which manipulates bobbing AND head "shaking".

    For making this more clear, i writed function to handle shaking effect. Its not rotating head, but player local position instead. I have commented the rotation line ( look for code, you see it ) so its easy to modify work with rotating , which wont work because mouse look script is forcing z axle to zero.

    Code (JavaScript):
    1. function ShakeHead(val : float, length : float)
    2. {
    3.     // set length of shake
    4.     ShakeLength = length;
    5.    
    6.     // Head shake      
    7.     if(ShakeWay == "None")
    8.     {
    9.         // Middle, start moving left first
    10.         ShakeWay = "Left";
    11.     }
    12.                                            
    13.     if( ShakeWay == "Left" && tmpshakevalue >= ShakeLength )
    14.     {
    15.         // Go middle and then move to left
    16.         tmpshakevalue = 0;
    17.         ShakeWay = "Middle";
    18.         tmpway = "Left";
    19.     }
    20.                        
    21.     if( ShakeWay == "Right" && tmpshakevalue >= ShakeLength  )
    22.     {
    23.         // Go middle and then move to right
    24.         tmpshakevalue = 0;
    25.         ShakeWay = "Middle";
    26.         tmpway = "Right";
    27.     }
    28.                        
    29.     if( ShakeWay == "Middle" && tmpshakevalue >= ShakeLength  )
    30.     {
    31.         tmpshakevalue = 0;
    32.         if(tmpway == "Left")
    33.         {
    34.             // Last time we was moving left, go now right
    35.             ShakeWay = "Right";
    36.         }
    37.         else
    38.         {
    39.             // vice versa
    40.             ShakeWay = "Left";
    41.         }                      
    42.     }
    43.  
    44.     // Look for ShakeWay
    45.     switch (ShakeWay)
    46.     {
    47.     case "Left":
    48.         //transform.localEulerAngles.z -= val;
    49.         transform.localPosition.x -= val;
    50.         tmpshakevalue += val;
    51.     break;
    52.                            
    53.     case "Right":
    54.         transform.localPosition.x += val;
    55.         tmpshakevalue += val;
    56.     break;
    57.  
    58.     case "Middle":
    59.         if(tmpway == "Left")
    60.         {
    61.             transform.localPosition.x += val;
    62.             tmpshakevalue += val;
    63.         }
    64.         else
    65.         {
    66.             transform.localPosition.x -= val;
    67.             tmpshakevalue += val;
    68.         }
    69.     break;                      
    70.     }
    71. }
     
    Last edited: Jun 24, 2014
  6. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    The camera object has both the mouse y & the head bobber, Put these on different objects.
    You sure are having a trouble understanding this...

    Here's how the scripts should be layed out in the hierarchy
     
    Last edited: Jun 24, 2014
    Mirace likes this.
  7. Mirace

    Mirace

    Joined:
    Nov 29, 2010
    Posts:
    481
    Thank you :) As i tought i knew what caused problem but i was too blind to see what caused that... It works now !
     
  8. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Glad I could help c:
     
    hkalterkait likes this.