Search Unity

Headbob Movement Issues

Discussion in 'Editor & General Support' started by Cinematronic, Jun 19, 2012.

  1. Cinematronic

    Cinematronic

    Joined:
    Oct 22, 2011
    Posts:
    22
    Episode 1.19 Headbob Movement



    Got a problem with my gunscript, I'm following Eteeski Tutorials on Youtube, I'm on episode 1.19 Headbob Movement,
    and when I follow the step of fixing the code to avoid the gun glitch (26:10 on video), when I change "function Update()" to "function LastUpdate()" in the gunscript, the gun just float on the air without moving or following the player at all.

    Any help will be truly appreciated.

    ; )

    here's my codes:

    Gunscript.js
    Code (csharp):
    1. var cameraObject : GameObject;
    2. @HideInInspector
    3. var targetXRotation : float;
    4. @HideInInspector
    5. var targetYRotation : float;
    6. @HideInInspector
    7. var targetXRotationV : float;
    8. @HideInInspector
    9. var targetYRotationV : float;
    10.  
    11. var rotateSpeed : float = 0.3;
    12.  
    13. var holdHeight : float = -0.5;
    14. var holdSide : float = 0.5;
    15. var racioHipHold : float = 1;
    16. var hipToAimSpeed : float = 0.1;
    17. @HideInInspector
    18. var racioHipHoldV : float;
    19.  
    20. var aimRacio : float = 0.4;
    21.  
    22. var zoomAngle : float = 30;
    23.  
    24. var fireSpeed : float = 15;
    25. @HideInInspector
    26. var waitTilNextFire : float = 0;
    27. var bullet : GameObject;
    28. var bulletSpawn : GameObject;
    29.  
    30. var shootAngleRandomizationAiming : float = 5;
    31. var shootAngleRandomizationNotAiming : float = 15;
    32.  
    33. var recoilAmount : float = 0.5;
    34. var recoilRecoverTime : float = 0.2;
    35. @HideInInspector
    36. var currentRecoilZPos : float;
    37. @HideInInspector
    38. var currentRecoilZPosV : float;
    39.  
    40. var bulletSound : GameObject;
    41. var muzzleFlash : GameObject;
    42.  
    43. function LastUpdate ()
    44. {
    45.     var holdMuzzleFlash : GameObject;
    46.     var holdSound : GameObject;
    47.     if (Input.GetButton("Fire1"))
    48.     {
    49.         if (waitTilNextFire <= 0)
    50.         {
    51.             if (bullet)
    52.                 Instantiate(bullet,bulletSpawn.transform.position, bulletSpawn.transform.rotation);
    53.             if (bulletSound)
    54.                 holdSound = Instantiate(bulletSound,bulletSpawn.transform.posi  tion, bulletSpawn.transform.rotation);
    55.             if (muzzleFlash)
    56.                 holdMuzzleFlash = Instantiate(muzzleFlash,bulletSpawn.transform.posi  tion, bulletSpawn.transform.rotation);
    57.             targetXRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, racioHipHold);
    58.             targetYRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, racioHipHold);
    59.             currentRecoilZPos -= recoilAmount;
    60.             waitTilNextFire = 1;
    61.         }
    62.     }
    63.     waitTilNextFire -= Time.deltaTime * fireSpeed;
    64.    
    65.     if (holdSound)
    66.         holdSound.transform.parent = transform;
    67.     if (holdMuzzleFlash)
    68.         holdMuzzleFlash.transform.parent = transform;
    69.    
    70.     currentRecoilZPos = Mathf.SmoothDamp( currentRecoilZPos, 0, currentRecoilZPosV, recoilRecoverTime);
    71.  
    72.     cameraObject.GetComponent(MouseLookScript).current  TargetCameraAngle = zoomAngle;
    73.  
    74.     if (Input.GetButton("Fire2")){
    75.         cameraObject.GetComponent(MouseLookScript).current  AimRacio = aimRacio;
    76.         racioHipHold = Mathf.SmoothDamp(racioHipHold, 0.5, racioHipHoldV, hipToAimSpeed);}
    77.     if (Input.GetButton("Fire2") == false){
    78.         cameraObject.GetComponent(MouseLookScript).current  AimRacio = 1;
    79.         racioHipHold = Mathf.SmoothDamp(racioHipHold, 1, racioHipHoldV, hipToAimSpeed);}
    80.        
    81.     transform.position = cameraObject.transform.position + (Quaternion.Euler(0,targetYRotation,0) * Vector3(holdSide * racioHipHold, holdHeight * racioHipHold,0) + Quaternion.Euler(targetXRotation, targetYRotation, 0) * Vector3(0,0,currentRecoilZPos));
    82.    
    83.     targetXRotation = Mathf.SmoothDamp( targetXRotation, cameraObject.GetComponent(MouseLookScript).xRotati  on, targetXRotationV, rotateSpeed);
    84.     targetYRotation = Mathf.SmoothDamp( targetYRotation, cameraObject.GetComponent(MouseLookScript).yRotati  on, targetYRotationV, rotateSpeed);
    85.    
    86.     transform.rotation = Quaternion.Euler(targetXRotation, targetYRotation, 0);
    87. }
    MouseLookScript.js
    Code (csharp):
    1. var defaultCameraAngle : float = 60;
    2. @HideInInspector
    3. var currentTargetCameraAngle : float = 60;
    4. @HideInInspector
    5. var racioZoom : float = 1;
    6. var racioZoomV : float;
    7. var racioZoomSpeed : float = 0.2;
    8.  
    9. var lookSensitivity : float = 5;
    10. @HideInInspector
    11. var yRotation : float;
    12. @HideInInspector
    13. var xRotation : float;
    14. @HideInInspector
    15. var currentYRotation : float;
    16. @HideInInspector
    17. var currentXRotation : float;
    18. @HideInInspector
    19. var yRotationV : float;
    20. @HideInInspector
    21. var xRotationV : float;
    22. var lookSmoothDamp : float = 0.1;
    23. @HideInInspector
    24. var currentAimRacio : float = 1;
    25.  
    26. var headbobSpeed : float = 1;
    27. @HideInInspector
    28. var headbobStepCounter : float;
    29. var headbobAmountX : float = 1;
    30. var headbobAmountY :  float = 1;
    31. @HideInInspector
    32. var parentLastPos : Vector3;
    33. var eyeHeightRacio : float = 0.9;
    34.  
    35. function Awake ()
    36. {
    37.     parentLastPos = transform.parent.position;
    38. }
    39.  
    40. function Update ()
    41. {  
    42.     if (transform.parent.GetComponent(PlayerMovementScrip  t).grounded)
    43.         headbobStepCounter += Vector3.Distance(parentLastPos, transform.parent.position) * headbobSpeed;
    44.     transform.localPosition.x = Mathf.Sin(headbobStepCounter) * headbobAmountX * currentAimRacio;
    45.     transform.localPosition.y = (Mathf.Cos(headbobStepCounter * 2) * headbobAmountY * -1 * currentAimRacio) + (transform.localScale.y * eyeHeightRacio) - (transform.localScale.y / 2);
    46.  
    47.     parentLastPos = transform.parent.position;
    48.  
    49.     if (currentAimRacio == 1)
    50.         racioZoom = Mathf.SmoothDamp(racioZoom, 1, racioZoomV, racioZoomSpeed);
    51.     else
    52.         racioZoom = Mathf.SmoothDamp(racioZoom, 0, racioZoomV, racioZoomSpeed);
    53.    
    54.     camera.fieldOfView = Mathf.Lerp(currentTargetCameraAngle, defaultCameraAngle, racioZoom);
    55.    
    56.     yRotation += Input.GetAxis("Mouse X") * lookSensitivity * currentAimRacio;
    57.     xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity * currentAimRacio;
    58.    
    59.     xRotation = Mathf.Clamp(xRotation, -90, 90);
    60.    
    61.     currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, xRotationV, lookSmoothDamp);
    62.     currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, yRotationV, lookSmoothDamp);
    63.    
    64.    
    65.     transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
    66. }
     
    Last edited: Jun 19, 2012
  2. BPPHarv

    BPPHarv

    Joined:
    Jun 9, 2012
    Posts:
    318
    I believe there might be a typo.. Try LateUpdate instead of LastUpdate. LateUpdate would be a standard unity function that ran after any Update functions, I have no idea what LastUpdate is.
     
  3. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    yep, I'm pretty sure there's no such thing as LastUpdate. The thing about that step is that I'm pretty sure the game works identically using both LateUpdate and Update. I might have been wrong in the tutorial.
     
  4. Cinematronic

    Cinematronic

    Joined:
    Oct 22, 2011
    Posts:
    22
    That's it!
    it was LateUpdate instead of LastUpdate, I don't know what I was trying to do. Now the glitch is corrected.
    At least in my version of the game, the glitch only shows with "function Update()"

    Thank you very much guys!