void Start() { //Set up external script references SmoothMouseLook = CameraObj.GetComponent <SmoothMouseLook>(); PlayerWeaponsComponent = weaponObj.GetComponent <PlayerWeapons>(); FPSWalker = playerObj.GetComponent <FPSRigidBodyWalker>(); VerticalBob = playerObj.GetComponent <VerticalBob>(); HorizontalBob = playerObj.GetComponent <HorizontalBob>(); FPSPlayerComponent = playerObj.GetComponent <FPSPlayer>(); InputComponent = playerObj.GetComponent <InputControl>(); WeaponPivotComponent = FPSPlayerComponent.WeaponPivotComponent; aSource = playerObj.AddComponent <AudioSource>(); aSource.spatialBlend = 0.0f; aSource.playOnAwake = false; }
void Start() { myTransform = transform; //define transform for efficiency //set up external script references FPSWalkerComponent = playerObj.GetComponent <FPSRigidBodyWalker>(); IronsightsComponent = playerObj.GetComponent <Ironsights>(); HorizontalBob = playerObj.GetComponent <HorizontalBob>(); FPSPlayerComponent = playerObj.GetComponent <FPSPlayer>(); InputComponent = playerObj.GetComponent <InputControl>(); CameraControlComponent = Camera.main.GetComponent <CameraControl>(); //initialize bobbing amounts walkBobYawAmount = Mathf.Clamp01(walkBobYawAmount); walkBobRollAmount = Mathf.Clamp01(walkBobRollAmount); sprintBobYawAmount = Mathf.Clamp01(sprintBobYawAmount); sprintBobRollAmount = Mathf.Clamp01(sprintBobRollAmount); gunBobRoll *= walkBobRollAmount; gunBobYaw *= walkBobYawAmount; }
void Update() { mainCamTransform = Camera.main.transform; //Set up external script references SmoothMouseLook SmoothMouseLook = CameraObj.GetComponent <SmoothMouseLook>(); PlayerWeapons PlayerWeaponsComponent = weaponObj.GetComponent <PlayerWeapons>(); FPSRigidBodyWalker FPSWalker = playerObj.GetComponent <FPSRigidBodyWalker>(); WeaponBehavior WeaponBehaviorComponent = gunObj.GetComponent <WeaponBehavior>(); VerticalBob VerticalBob = playerObj.GetComponent <VerticalBob>(); HorizontalBob HorizontalBob = playerObj.GetComponent <HorizontalBob>(); FPSPlayer FPSPlayerComponent = playerObj.GetComponent <FPSPlayer>(); if (Time.timeScale > 0 && Time.deltaTime > 0) //allow pausing by setting timescale to 0 //main weapon position smoothing happens here { newPosX = Mathf.SmoothDamp(newPosX, nextPosX, ref dampVel.x, yDampSpeed, Mathf.Infinity, Time.deltaTime); newPosY = Mathf.SmoothDamp(newPosY, nextPosY, ref dampVel.y, yDampSpeed, Mathf.Infinity, Time.deltaTime); newPosZ = Mathf.SmoothDamp(newPosZ, nextPosZ, ref dampVel.z, zDampSpeed, Mathf.Infinity, Time.deltaTime); zPosRec = Mathf.SmoothDamp(zPosRec, zPosRecNext, ref recZDamp, 0.25f, Mathf.Infinity, Time.deltaTime); //smooth recoil kick back of weapon newFov = Mathf.SmoothDamp(Camera.main.fieldOfView, nextFov, ref dampFOV, FovSmoothSpeed, Mathf.Infinity, Time.deltaTime); //smooth camera FOV //Sync camera FOVs WeapCameraObj.fieldOfView = Camera.main.fieldOfView - weaponCamFovDiff; Camera.main.fieldOfView = newFov; //Get input from player movement script float horizontal = FPSWalker.inputX; float vertical = FPSWalker.inputY; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Adjust weapon position and bobbing amounts dynamicly based on movement and player states ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //move weapon back towards camera based on kickBack amount in WeaponBehavior.cs if (WeaponBehaviorComponent.shootStartTime + 0.1f > Time.time) { if (FPSPlayerComponent.zoomed) { zPosRecNext = WeaponBehaviorComponent.kickBackAmtZoom; } else { zPosRecNext = WeaponBehaviorComponent.kickBackAmtUnzoom; } } else { zPosRecNext = 0.0f; } if (Mathf.Abs(horizontal) != 0 || ((!FPSPlayerComponent.useAxisInput && (Input.GetKey(FPSPlayerComponent.moveForward) || Input.GetKey(FPSPlayerComponent.moveBack))) || (FPSPlayerComponent.useAxisInput && Mathf.Abs(vertical) > 0.1f))) { idleY = 0; idleX = 0; //check for sprinting if (FPSWalker.sprintActive && !FPSPlayerComponent.zoomed && !FPSWalker.crouched && FPSWalker.midPos >= FPSWalker.standingCamHeight && //player might not have completely stood up yet from crouch !((Mathf.Abs(horizontal) != 0.0f) && (Mathf.Abs(vertical) < 0.75f)) && !FPSWalker.cancelSprint) { sprintBob = 128.0f; if (!FPSWalker.cancelSprint && !reloading && !FPSWalker.jumping && FPSWalker.fallingDistance < 0.75f) //actually sprinting now //set the camera's fov back to normal if the player has sprinted into a wall, but the sprint is still active { if (FPSWalker.inputY != 0) { nextFov = sprintFov; } else { nextFov = defaultFov; } //gradually move weapon more towards center while sprinting weaponSprintXPositionAmt = Mathf.MoveTowards(weaponSprintXPositionAmt, WeaponBehaviorComponent.weaponSprintXPosition, Time.deltaTime * 16); horizontalGunPosAmt = WeaponBehaviorComponent.weaponUnzoomXPosition + weaponSprintXPositionAmt; gunRunSide = 2.0f; if (gunRunUp < 1.4f) { gunRunUp += Time.deltaTime / 4.0f; } //gradually increase for smoother transition bobMove = gunup + WeaponBehaviorComponent.weaponSprintYPosition; //raise weapon while sprinting } else //not sprinting { nextFov = defaultFov; horizontalGunPosAmt = WeaponBehaviorComponent.weaponUnzoomXPosition; gunRunSide = 1.0f; gunRunUp = 1.0f; bobMove = -0.01f; switchMove = 0.0f; } } else //walking { gunRunSide = 1.0f; gunRunUp = 1.0f; //reset horizontal weapon positioning var and make sure it returns to zero when not sprinting to prevent unwanted side movement weaponSprintXPositionAmt = Mathf.MoveTowards(weaponSprintXPositionAmt, 0, Time.deltaTime * 16); horizontalGunPosAmt = WeaponBehaviorComponent.weaponUnzoomXPosition + weaponSprintXPositionAmt; if (reloading) //move weapon position up when reloading and moving for full view of animation { nextFov = defaultFov; sprintBob = 216; bobMove = 0.0F; sideMove = -0.0f; } else { nextFov = defaultFov; if (FPSPlayerComponent.zoomed && WeaponBehaviorComponent.meleeSwingDelay == 0) //zoomed and not melee weapon { sprintBob = 96.0f; // if (Mathf.Abs(horizontal) != 0 || Mathf.Abs(vertical) > 0.75f){ // bobMove = -0.001f;//move weapon down // }else{ bobMove = 0.0F; //move weapon to idle // } } else //not zoomed { sprintBob = 216.0f; if (Mathf.Abs(horizontal) != 0 || Mathf.Abs(vertical) > 0.75f) { //move weapon down and left when crouching if (FPSWalker.crouched || FPSWalker.midPos < FPSWalker.standingCamHeight * 0.85f) { bobMove = -0.01f; sideMove = -0.0125f; } else { bobMove = -0.005f; sideMove = -0.00f; } } else { //move weapon to idle bobMove = 0.0F; sideMove = 0.0F; } } } } } else //if not moving (no player movement input) { nextFov = defaultFov; horizontalGunPosAmt = WeaponBehaviorComponent.weaponUnzoomXPosition; if (weaponSprintXPositionAmt > 0) { weaponSprintXPositionAmt -= Time.deltaTime / 4; } sprintBob = 96.0f; if (reloading) { nextFov = defaultFov; sprintBob = 96.0f; bobMove = 0.0F; sideMove = -0.0f; } else { //move weapon to idle if ((FPSWalker.crouched || FPSWalker.midPos < FPSWalker.standingCamHeight * 0.85f) && !FPSPlayerComponent.zoomed) { bobMove = -0.005f; sideMove = -0.0125f; } else { bobMove = 0.0f; sideMove = 0.0f; } } //weapon idle motion if (FPSPlayerComponent.zoomed && WeaponBehaviorComponent.meleeSwingDelay == 0) { idleX = Mathf.Sin(Time.time * 1.25f) / 4800.0f; idleY = Mathf.Sin(Time.time * 1.5f) / 4800.0f; } else { if (!FPSWalker.swimming) { idleX = Mathf.Sin(Time.time * 1.25f) / 800.0f; idleY = Mathf.Sin(Time.time * 1.5f) / 800.0f; } else { idleX = Mathf.Sin(Time.time * 1.25f) / 400.0f; idleY = Mathf.Sin(Time.time * 1.5f) / 400.0f; } } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Weapon Swaying/Bobbing while moving ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //track X axis while walking for side to side bobbing effect if (HorizontalBob.waveslice != 0) { bobDir = 1; } else { bobDir = -1; } //Reduce weapon bobbing by sprintBobAmount value defined in WeaponBehavior script. //This is for fine tuning of weapon bobbing. Pistols look better with less sprint bob //because they use a different sprinting anim and have a different sprinting position //than the animation used by rifle-type weapons. if (!FPSWalker.canRun) { sprintBobAmtX = sprintBob / WeaponBehaviorComponent.walkBobAmountX; sprintBobAmtY = sprintBob / WeaponBehaviorComponent.walkBobAmountY; } else { sprintBobAmtX = sprintBob / WeaponBehaviorComponent.sprintBobAmountX; sprintBobAmtY = sprintBob / WeaponBehaviorComponent.sprintBobAmountY; } //set smoothed weapon position to actual gun position vector tempGunPos.x = newPosX; tempGunPos.y = newPosY; tempGunPos.z = newPosZ + zPosRec; //add weapon z position and recoil kick back //apply temporary vector to gun's transform position gun.localPosition = tempGunPos; //lower weapon when jumping, falling, or slipping off ledge if (FPSWalker.jumping || FPSWalker.fallingDistance > 1.25f) { //lower weapon less when zoomed if (!FPSPlayerComponent.zoomed) { //raise weapon when jump is ascending and lower when descending if ((FPSWalker.airTime + 0.175f) > Time.time) { jumpmove = 0.015f; } else { jumpmove = -0.025f; } } else { jumpmove = -0.01f; } } else { jumpmove = 0.0f; } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Adjust vars for zoom and other states ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// float deltaAmount = Time.deltaTime * 100; //define delta for framerate independence float bobDeltaAmount = 0.12f / Time.deltaTime; //define bobbing delta for framerate independence if (!WeaponBehaviorComponent.PistolSprintAnim || !FPSWalker.canRun) { gunAnimTime = gunObj.GetComponent <Animation>()["RifleSprinting"].normalizedTime; //Track playback position of rifle sprinting animation } else { gunAnimTime = gunObj.GetComponent <Animation>()["PistolSprinting"].normalizedTime; //Track playback position of pistol sprinting animation } //if zoomed //check time of weapon sprinting anim to make weapon return to center, then zoom normally if (FPSPlayerComponent.zoomed && FPSPlayerComponent.hitPoints > 1.0f && PlayerWeaponsComponent.switchTime + WeaponBehaviorComponent.readyTime < Time.time && //don't raise sights when readying weapon !reloading && gunAnimTime < 0.35f && WeaponBehaviorComponent.meleeSwingDelay == 0 && //not a melee weapon PlayerWeaponsComponent.currentWeapon != 0 && WeaponBehaviorComponent.reloadLastStartTime + WeaponBehaviorComponent.reloadLastTime < Time.time) { //adjust FOV and weapon position for zoom nextFov = WeaponBehaviorComponent.zoomFOV; FovSmoothSpeed = 0.09f; //faster FOV zoom speed when zooming in yDampSpeed = 0.09f; zDampSpeed = 0.15f; //X pos with idle movement nextPosX = WeaponBehaviorComponent.weaponZoomXPosition + (side / 1.5f) + idleX //also add X bobbing amounts here so they get smoothed to remove any small glitches in position + (((HorizontalBob.translateChange / sprintBobAmtX) * bobDeltaAmount) * gunRunSide * bobDir); //Y pos with idle movement nextPosY = WeaponBehaviorComponent.weaponZoomYPosition + (raise / 1.5f) + idleY + (bobMove + switchMove + climbMove + jumpAmt + jumpmove) //also add Y bobbing amounts here so they get smoothed to remove any small glitches in position + (((VerticalBob.translateChange / sprintBobAmtY) * bobDeltaAmount) * gunRunUp * bobDir); //Z pos nextPosZ = WeaponBehaviorComponent.weaponZoomZPosition; //slow down turning and movement speed for zoom FPSWalker.zoomSpeed = true; //If not a melee weapon, play sound effect when raising sights if (zoomSfxState && WeaponBehaviorComponent.meleeSwingDelay == 0 && !WeaponBehaviorComponent.unarmed) { AudioSource.PlayClipAtPoint(sightsUpSnd, mainCamTransform.position); zoomSfxState = false; } //Reduce mouse sensitivity when zoomed, but maintain sensitivity set in SmoothMouseLook script SmoothMouseLook.sensitivityAmt = SmoothMouseLook.sensitivity * zoomSensitivity; //Gradually increase or decrease bobbing amounts for smooth transitions between movement states ////zoomed bobbing amounts//// //horizontal bobbing if (GBamt > ZoomHorizontalBobAmount) { GBamt -= 0.005f * deltaAmount; } if (GBamt < ZoomHorizontalBobAmount) { GBamt += 0.005f * deltaAmount; } //vertical bobbing if (HBamt > ZoomVerticalBobAmount) { HBamt -= 0.005f * deltaAmount; } if (HBamt < ZoomVerticalBobAmount) { HBamt += 0.005f * deltaAmount; } //pitching if (HPamt > ZoomBobPitchAmount) { HPamt -= 0.0075f * deltaAmount; } if (HPamt < ZoomBobPitchAmount) { HPamt += 0.0075f * deltaAmount; } //rolling if (HRamt > ZoomBobRollAmount) { HRamt -= 0.0075f * deltaAmount; } if (HRamt < ZoomBobRollAmount) { HRamt += 0.0075f * deltaAmount; } //yawing if (HYamt > ZoomBobYawAmount) { HYamt -= 0.0075f * deltaAmount; } if (HYamt < ZoomBobYawAmount) { HYamt += 0.0075f * deltaAmount; } if (!FPSWalker.swimming) { //Set bobbing speeds and amounts in other scripts to these smoothed values VerticalBob.bobbingSpeed = ZoomBobSpeed; //make horizontal bob speed half as slow as vertical bob speed for synchronization of bobbing motions HorizontalBob.bobbingSpeed = ZoomBobSpeed / 2.0f; } else { //Set bobbing speeds and amounts in other scripts to these smoothed values VerticalBob.bobbingSpeed = ZoomBobSpeed / 2.0f; //make horizontal bob speed half as slow as vertical bob speed for synchronization of bobbing motions HorizontalBob.bobbingSpeed = ZoomBobSpeed / 4.0f; } VerticalBob.bobbingAmount = HBamt * deltaAmount; //apply delta at this step for framerate independence VerticalBob.rollingAmount = HRamt * deltaAmount; VerticalBob.yawingAmount = HYamt * deltaAmount; VerticalBob.pitchingAmount = HPamt * deltaAmount; HorizontalBob.bobbingAmount = GBamt * deltaAmount; } else //not zoomed { FovSmoothSpeed = 0.18f; //slower FOV zoom speed when zooming out //adjust weapon Y position smoothing speed for unzoom and switching weapons if (!PlayerWeaponsComponent.switching) { yDampSpeed = 0.18f; //weapon swaying speed } else { yDampSpeed = 0.2f; //weapon switch raising speed } zDampSpeed = 0.1f; //X pos with idle movement nextPosX = side + idleX + sideMove + horizontalGunPosAmt //also add X bobbing amounts here so they get smoothed to remove any small glitches in position + (((HorizontalBob.translateChange / sprintBobAmtX) * bobDeltaAmount) * gunRunSide * bobDir); //Y pos with idle movement nextPosY = raise + idleY + (bobMove + climbMove + switchMove + jumpAmt + jumpmove) + WeaponBehaviorComponent.weaponUnzoomYPosition //also add Y bobbing amounts here so they get smoothed to remove any small glitches in position + (((VerticalBob.translateChange / sprintBobAmtY) * bobDeltaAmount) * gunRunUp * bobDir); //Z pos nextPosZ = WeaponBehaviorComponent.weaponUnzoomZPosition; //Set turning and movement speed for unzoom FPSWalker.zoomSpeed = false; //If not a melee weapon, play sound effect when lowering sights if (!zoomSfxState && WeaponBehaviorComponent.meleeSwingDelay == 0 && !WeaponBehaviorComponent.unarmed) { AudioSource.PlayClipAtPoint(sightsDownSnd, mainCamTransform.position); zoomSfxState = true; } //Return mouse sensitivity to normal SmoothMouseLook.sensitivityAmt = SmoothMouseLook.sensitivity; //Set weapon and view bobbing amounts if (FPSWalker.sprintActive && !((Mathf.Abs(horizontal) != 0.0f) && (Mathf.Abs(vertical) < 0.75f)) && Mathf.Abs(vertical) != 0.0f && !FPSWalker.cancelSprint && !FPSWalker.crouched && FPSWalker.midPos >= FPSWalker.standingCamHeight && !FPSPlayerComponent.zoomed && !Input.GetKey(FPSPlayerComponent.fire)) { //scale up bob speeds slowly to prevent jerky transition if (FPSWalker.grounded) { ////sprinting bobbing amounts//// //horizontal bobbing if (GBamt < SprintHorizontalBobAmount) { GBamt += 0.005f * deltaAmount; } if (GBamt > SprintHorizontalBobAmount) { GBamt -= 0.005f * deltaAmount; } //vertical bobbing if (HBamt < SprintVerticalBobAmount) { HBamt += 0.005f * deltaAmount; } if (HBamt > SprintVerticalBobAmount) { HBamt -= 0.005f * deltaAmount; } //pitching if (HPamt < SprintBobPitchAmount) { HPamt += 0.0075f * deltaAmount; } if (HPamt > SprintBobPitchAmount) { HPamt -= 0.0075f * deltaAmount; } //rolling if (HRamt < SprintBobRollAmount) { HRamt += 0.0075f * deltaAmount; } if (HRamt > SprintBobRollAmount) { HRamt -= 0.0075f * deltaAmount; } //yawing if (HYamt < SprintBobYawAmount) { HYamt += 0.0075f * deltaAmount; } if (HYamt > SprintBobYawAmount) { HYamt -= 0.0075f * deltaAmount; } } else { //reduce bobbing amounts for smooth jumping/landing transition if (HBamt > 0.0f) { HBamt -= 0.01f * deltaAmount; } if (HRamt > 0.0f) { HRamt -= 0.02f * deltaAmount; } if (HYamt > 0.0f) { HYamt -= 0.02f * deltaAmount; } if (HPamt > 0.0f) { HPamt -= 0.02f * deltaAmount; } if (GBamt > 0.0f) { GBamt -= 0.01f * deltaAmount; } } //Set bobbing speeds and amounts in other scripts to these smoothed values VerticalBob.bobbingSpeed = SprintBobSpeed; //make horizontal bob speed half as slow as vertical bob speed for synchronization of bobbing motions HorizontalBob.bobbingSpeed = SprintBobSpeed / 2.0f; HorizontalBob.bobbingAmount = GBamt * deltaAmount; //apply delta at this step for framerate independence VerticalBob.rollingAmount = HRamt * deltaAmount; VerticalBob.yawingAmount = HYamt * deltaAmount; VerticalBob.pitchingAmount = HPamt * deltaAmount; VerticalBob.bobbingAmount = HBamt * deltaAmount; //move weapon toward or away from camera while sprinting nextPosZ = WeaponBehaviorComponent.weaponSprintZPosition; } else { //scale up bob speeds slowly to prevent jerky transition if (FPSWalker.grounded) { if (!FPSWalker.crouched && FPSWalker.midPos >= FPSWalker.standingCamHeight) { ////walking bob amounts/// //horizontal bobbing if (GBamt < WalkHorizontalBobAmount) { GBamt += 0.005f * deltaAmount; } if (GBamt > WalkHorizontalBobAmount) { GBamt -= 0.005f * deltaAmount; } //vertical bobbing if (HBamt < WalkVerticalBobAmount) { HBamt += 0.005f * deltaAmount; } if (HBamt > WalkVerticalBobAmount) { HBamt -= 0.005f * deltaAmount; } //pitching if (HPamt < WalkBobPitchAmount) { HPamt += 0.0075f * deltaAmount; } if (HPamt > WalkBobPitchAmount) { HPamt -= 0.0075f * deltaAmount; } //rolling if (!FPSWalker.swimming) { if (HRamt < WalkBobRollAmount) { HRamt += 0.0075f * deltaAmount; } if (HRamt > WalkBobRollAmount) { HRamt -= 0.0075f * deltaAmount; } } else { if (HRamt < WalkBobRollAmount * 2.0f) { HRamt += 0.0075f * deltaAmount; } if (HRamt > WalkBobRollAmount * 2.0f) { HRamt -= 0.0075f * deltaAmount; } } //yawing if (HYamt < WalkBobYawAmount) { HYamt += 0.0075f * deltaAmount; } if (HYamt > WalkBobYawAmount) { HYamt -= 0.0075f * deltaAmount; } if (!FPSWalker.swimming) { VerticalBob.bobbingSpeed = WalkBobSpeed; //make horizontal bob speed half as slow as vertical bob speed for synchronization of bobbing motions HorizontalBob.bobbingSpeed = WalkBobSpeed / 2.0f; } else //bobbing is slower while swimming { VerticalBob.bobbingSpeed = WalkBobSpeed / 2; HorizontalBob.bobbingSpeed = WalkBobSpeed / 4.0f; } } else { ////crouching bob amounts//// //horizontal bobbing if (GBamt < CrouchHorizontalBobAmount) { GBamt += 0.005f * deltaAmount; } if (GBamt > CrouchHorizontalBobAmount) { GBamt -= 0.005f; } //vertical bobbing if (HBamt < CrouchVerticalBobAmount) { HBamt += 0.005f * deltaAmount; } if (HBamt > CrouchVerticalBobAmount) { HBamt -= 0.005f * deltaAmount; } //pitching if (HPamt < CrouchBobPitchAmount) { HPamt += 0.0075f * deltaAmount; } if (HPamt > CrouchBobPitchAmount) { HPamt -= 0.0075f * deltaAmount; } //rolling if (HRamt < CrouchBobRollAmount) { HRamt += 0.0075f * deltaAmount; } if (HRamt > CrouchBobRollAmount) { HRamt -= 0.0075f * deltaAmount; } //yawing if (HYamt < CrouchBobYawAmount) { HYamt += 0.0075f * deltaAmount; } if (HYamt > CrouchBobYawAmount) { HYamt -= 0.0075f * deltaAmount; } VerticalBob.bobbingSpeed = CrouchBobSpeed; HorizontalBob.bobbingSpeed = CrouchBobSpeed / 2.0f; } } else { //reduce bobbing amounts for smooth jumping/landing transition if (HBamt > 0.0f) { HBamt -= 0.01f * deltaAmount; } if (HRamt > 0.0f) { HRamt -= 0.02f * deltaAmount; } if (HYamt > 0.0f) { HYamt -= 0.02f * deltaAmount; } if (HPamt > 0.0f) { HPamt -= 0.02f * deltaAmount; } if (GBamt > 0.0f) { GBamt -= 0.01f * deltaAmount; } } VerticalBob.bobbingAmount = GBamt * deltaAmount; //apply delta at this step for framerate independence VerticalBob.rollingAmount = HRamt * deltaAmount; VerticalBob.yawingAmount = HYamt * deltaAmount; VerticalBob.pitchingAmount = HPamt * deltaAmount; HorizontalBob.bobbingAmount = HBamt * deltaAmount; } } } }
void Update() { if (Time.timeScale > 0 && Time.deltaTime > 0) //allow pausing by setting timescale to 0 //set up external script references { FPSRigidBodyWalker FPSWalkerComponent = playerObj.GetComponent <FPSRigidBodyWalker>(); Ironsights IronsightsComponent = playerObj.GetComponent <Ironsights>(); WeaponBehavior WeaponBehaviorComponent = IronsightsComponent.gunObj.GetComponent <WeaponBehavior>(); HorizontalBob HorizontalBob = playerObj.GetComponent <HorizontalBob>(); FPSPlayer FPSPlayerComponent = playerObj.GetComponent <FPSPlayer>(); if (FPSWalkerComponent.canRun) //sprinting //set sway amounts for sprinting { swingAmt = 0.02f * swayAmount * WeaponBehaviorComponent.swayAmountUnzoomed; swingSpeed = 0.000025f * swayAmount * WeaponBehaviorComponent.swayAmountUnzoomed; rollSpeed = 0.0f * rollSwayAmount * WeaponBehaviorComponent.swayAmountUnzoomed; //smoothly change bobbing amounts for sprinting if (gunBobYaw < 12.0f * sprintBobYawAmount) { gunBobYaw += 60.0f * Time.deltaTime; } if (gunBobRoll < 15.0f * sprintBobRollAmount) { gunBobRoll += 60.0f * Time.deltaTime; } } else //walking //smoothly change bobbing amounts for walking { if (gunBobYaw > -16.0f * walkBobYawAmount) { gunBobYaw -= 60.0f * Time.deltaTime; } if (gunBobRoll > 10.0f * walkBobRollAmount) { gunBobRoll -= 60.0f * Time.deltaTime; } //set sway amounts based on player state if (!FPSPlayerComponent.zoomed || IronsightsComponent.reloading || WeaponBehaviorComponent.meleeSwingDelay != 0) { swingAmt = 0.035f * swayAmount * WeaponBehaviorComponent.swayAmountUnzoomed; swingSpeed = 0.00015f * swayAmount * WeaponBehaviorComponent.swayAmountUnzoomed; rollSpeed = 0.025f * rollSwayAmount * WeaponBehaviorComponent.swayAmountUnzoomed; } else { swingAmt = 0.025f * swayAmount * WeaponBehaviorComponent.swayAmountZoomed; swingSpeed = 0.0001f * swayAmount * WeaponBehaviorComponent.swayAmountZoomed; rollSpeed = 0.075f * rollSwayAmount * WeaponBehaviorComponent.swayAmountZoomed; } } //use both these z axes for independent animation of camera X and Y position and bobbing of camera roll zAxis1 = Camera.main.transform.localEulerAngles.z; zAxis2 = cameraObj.transform.localEulerAngles.z; //Set target angles to main camera angles targetRotation.x = cameraObj.transform.localEulerAngles.x; targetRotation.y = cameraObj.transform.localEulerAngles.y; targetRotation.z = Mathf.MoveTowardsAngle(zAxis1, zAxis2, Time.deltaTime / 16); //sway gun //find angle delta and reduce to smaller value localSide = Mathf.DeltaAngle(targetRotation2.y, targetRotation.y) * -(swingSpeed / Time.deltaTime); //pass sway value to ironsights script where gun position is calculated IronsightsComponent.side = Mathf.Clamp(localSide, -swingAmt, swingAmt); //find angle delta and reduce to smaller value localRaise = Mathf.DeltaAngle(targetRotation2.x, targetRotation.x) * (swingSpeed / Time.deltaTime); //pass sway value to ironsights script where gun position is calculated IronsightsComponent.raise = Mathf.Clamp(localRaise, -swingAmt, swingAmt); //calculate roll swaying of weapon and smooth with lerpAngle to minimize snapping of gun during extreme rotations localRoll = Mathf.LerpAngle(localRoll, Mathf.DeltaAngle(targetRotationRoll, targetRotation.y) * -rollSpeed * 3.0f, Time.deltaTime * 5.0f); //dampen angles of gun with main camera angles for swaying effects targetRotation2.x = Mathf.SmoothDampAngle(targetRotation2.x, targetRotation.x, ref dampVelocity1, dampSpeed, Mathf.Infinity, Time.deltaTime); targetRotation2.y = Mathf.SmoothDampAngle(targetRotation2.y, targetRotation.y, ref dampVelocity2, dampSpeed, Mathf.Infinity, Time.deltaTime); targetRotationRoll = Mathf.SmoothDampAngle(targetRotationRoll, targetRotation.y, ref dampVelocity6, 0.075f, Mathf.Infinity, Time.deltaTime); //set gun angles //set temporary vector to smoothed angles of gun and add yaw and roll bobbing and roll swaying Vector3 tempEulerAngles = new Vector3( (targetRotation.x), (targetRotation.y + (HorizontalBob.dampOrg * gunBobYaw)), (targetRotation.z + localRoll + (HorizontalBob.dampOrg * -gunBobRoll)) ); //finally, set actual weapon angles to the temporary vector's angles myTransform.localEulerAngles = tempEulerAngles; } }