void Start() { //set up object and component references playerObj = Camera.main.transform.GetComponent <CameraControl>().playerObj; myTransform = transform;//store this object's transform for optimization playerObjTransform = playerObj.transform; mainCameraTransform = Camera.main.transform; FPSPlayerComponent = playerObj.GetComponent <FPSPlayer>(); weaponObj = FPSPlayerComponent.weaponObj; MouseLookComponent = transform.parent.transform.GetComponent <SmoothMouseLook>(); GunSwayComponent = weaponObj.GetComponent <GunSway>(); FPSWalkerComponent = playerObj.GetComponent <FPSRigidBodyWalker>(); VisibleBodyComponent = FPSWalkerComponent.VisibleBody.GetComponent <VisibleBody>(); IronsightsComponent = playerObj.GetComponent <Ironsights>(); InputComponent = playerObj.GetComponent <InputControl>(); if (playerObj.GetComponent <WorldRecenter>()) { WorldRecenterComponent = playerObj.GetComponent <WorldRecenter>(); } AnimationComponent = GetComponent <Animation>(); offsetAmt = offset; currentDistance = 0f; zoomDistance = 0f; }
public void Start() { weaponObj = transform.gameObject; PlayerWeaponsComponent = weaponObj.GetComponentInChildren <PlayerWeapons>(); WeaponBehaviorComponent = PlayerWeaponsComponent.CurrentWeaponBehaviorComponent; FPSWalkerComponent = Camera.main.transform.GetComponent <CameraControl>().playerObj.GetComponent <FPSRigidBodyWalker>(); }
void Start() { //set up external script references playerObj = Camera.main.transform.GetComponent <CameraControl>().playerObj; FPSWalkerComponent = playerObj.GetComponent <FPSRigidBodyWalker>(); CameraControlComponent = Camera.main.GetComponent <CameraControl>(); }
void Start() { if (GetComponent <FPSRigidBodyWalker>()) { fpsRigid = GetComponent <FPSRigidBodyWalker>(); } }
void Die() { FPSRigidBodyWalker FPSWalkerComponent = GetComponent <FPSRigidBodyWalker>(); bulletTimeActive = false; //set bulletTimeActive to false so fadeout wont take longer if bullet time is active if (!FPSWalkerComponent.drowning) { AudioSource.PlayClipAtPoint(die, mainCamTransform.position); //play normal player death sound effect if the player is on land } else { AudioSource.PlayClipAtPoint(dieDrown, mainCamTransform.position); //play drowning sound effect if the player is underwater } //disable player control and sprinting on death FPSWalkerComponent.inputX = 0; FPSWalkerComponent.inputY = 0; FPSWalkerComponent.cancelSprint = true; GameObject llf = Instantiate(levelLoadFadeObj) as GameObject; //Create instance of levelLoadFadeObj //call FadeAndLoadLevel function with fadein argument set to false //in levelLoadFadeObj to restart level and fade screen out from black on level load llf.GetComponent <LevelLoadFade>().FadeAndLoadLevel(Color.black, 2.0f, false); }
// Use this for initialization void Start() { if (GameObject.Find("targetManager") != null) { targetManager = GameObject.Find("targetManager"); } FPSWalker = playerObj.Value.GetComponent <FPSRigidBodyWalker>(); }
void Start() { FPSWalkerComponent = GetComponent <FPSRigidBodyWalker>(); FPSPlayerComponent = GetComponent <FPSPlayer>(); mainCamTransform = Camera.main.transform; //proportionately scale reachDistance by playerHeightMod amount reachDistanceAmt = reachDistance / (1 - (FPSWalkerComponent.playerHeightMod / FPSWalkerComponent.capsule.height)); }
protected void Start() { playerPhysics = player.GetComponent <FPSRigidBodyWalker>(); grapplePlayerBody = grapplePartJoint.GetComponent <Rigidbody>(); grappleSwingJoint = grapplePartBase.GetComponent <HingeJoint>(); }
public void FootstepSfx() { FPSRigidBodyWalker FPSWalkerComponent = playerObj.GetComponent <FPSRigidBodyWalker>(); //play footstep sound effects if (!FPSWalkerComponent.climbing) { if (FPSWalkerComponent.inWater) //play swimming/wading footstep effects { footStepClip = waterSounds[Random.Range(0, waterSounds.Length)]; //select random water step effect from waterSounds array volumeAmt = waterSoundVol; //set volume of audio clip to customized amount AudioSource.PlayClipAtPoint(footStepClip, cameraTransform.position, volumeAmt); } else { //Make a short delay before playing footstep sounds to allow landing sound to play if (FPSWalkerComponent.grounded && (FPSWalkerComponent.landStartTime + 0.4f) < Time.time) { switch (materialType) //determine which material the player is standing on and select random footstep effect for surface type { case "Wood": footStepClip = woodSteps[Random.Range(0, woodSteps.Length)]; volumeAmt = woodStepVol; break; case "Metal": footStepClip = metalSteps[Random.Range(0, metalSteps.Length)]; volumeAmt = metalStepVol; break; case "Dirt": footStepClip = dirtSteps[Random.Range(0, dirtSteps.Length)]; volumeAmt = dirtStepVol; break; case "Stone": footStepClip = stoneSteps[Random.Range(0, stoneSteps.Length)]; volumeAmt = stoneStepVol; break; default: footStepClip = dirtSteps[Random.Range(0, dirtSteps.Length)]; volumeAmt = dirtStepVol; break; } //play the sound effect AudioSource.PlayClipAtPoint(footStepClip, cameraTransform.position, volumeAmt); } } } else //play climbing footstep effects { footStepClip = climbSounds[Random.Range(0, climbSounds.Length)]; volumeAmt = climbSoundVol; AudioSource.PlayClipAtPoint(footStepClip, cameraTransform.position, volumeAmt); } }
void FixedUpdate() { //set up external script references FPSRigidBodyWalker FPSWalkerComponent = playerObj.GetComponent <FPSRigidBodyWalker>(); PlayerWeapons PlayerWeaponsComponent = gunObj.transform.parent.GetComponent <PlayerWeapons>(); if (Time.time > shellRemovalTime) { Object.Destroy(lerpShell.gameObject); Object.Destroy(gameObject); } //don't add rotation until a short time after shell is ejected for visual effect and stop adding torque after a time if (startTime + Time.fixedDeltaTime > Time.time) { //gradually increase rotation amount for smooth rotation transition rotateAmt = 0.1f; //apply torque to rigidbody rigidbody.AddRelativeTorque(Vector3.up * (Random.Range(rotateAmt * 1.75f, rotateAmt) * shellRotateSide)); rigidbody.AddRelativeTorque(Vector3.right * (Random.Range(rotateAmt * 4, rotateAmt * 6) * shellRotateUp)); } //Check if the player is on a moving platform to determine how to handle shell parenting and velocity if (playerObjTransform.parent == null) //if player is not on a moving platform //Make the shell's parent the weapon object for a short time after ejection //to the link shell ejection position with weapon object for more consistent movement, { if (((startTime + 0.35f < Time.time && parentState) //don't parent shell if switching weapon || (PlayerWeaponsComponent.switching && parentState) //don't parent shell if moving weapon to sprinting position || (FPSWalkerComponent.sprintActive && !FPSWalkerComponent.cancelSprint && parentState)) && FPSWalkerComponent.grounded) { Vector3 tempVelocity = playerObjTransform.rigidbody.velocity; tempVelocity.y = 0.0f; myTransform.parent = null; lerpShell.parent = null; //add player velocity to shell when unparenting from player object to prevent shell from suddenly changing direction if (!FPSWalkerComponent.sprintActive && !FPSWalkerComponent.canRun) //don't inherit parent velocity if sprinting to prevent visual glitches { rigidbody.AddForce(tempVelocity, ForceMode.VelocityChange); } parentState = false; } } else //if player is on elevator, keep gun object as parent for a longer time to prevent strange shell movements { if (startTime + 0.5f < Time.time && parentState) { myTransform.parent = null; lerpShell.parent = null; //add player velocity to shell when unparenting from player object to prevent shell from suddenly changing direction rigidbody.AddForce(playerObjTransform.rigidbody.velocity, ForceMode.VelocityChange); } } }
void Start() { myTransform = transform;//store this object's transform for optimization playerObjTransform = playerObj.transform; mainCameraTransform = Camera.main.transform; //define external script references FPSWalkerComponent = playerObj.GetComponent <FPSRigidBodyWalker>(); IronsightsComponent = playerObj.GetComponent <Ironsights>(); FPSPlayerComponent = playerObj.GetComponent <FPSPlayer>(); WorldRecenterComponent = playerObj.GetComponent <WorldRecenter>(); }
void OnTriggerExit(Collider other) { //on exit of vault trigger, deactivate trigger to prevent player from falling on it from above and hovering if (other.gameObject.tag == "Player") { FPSRigidBodyWalker FPSWalkerComponent = playerObj.GetComponent <FPSRigidBodyWalker>(); FPSWalkerComponent.climbing = false; FPSWalkerComponent.noClimbingSfx = false; transform.gameObject.GetComponent <BoxCollider>().enabled = false; } }
void Start() { //Set up external script references playerObj = Camera.main.transform.GetComponent <CameraControl>().playerObj; FPSWalkerComponent = playerObj.GetComponent <FPSRigidBodyWalker>(); IronsightsComponent = playerObj.GetComponent <Ironsights>(); CameraControlComponent = Camera.main.GetComponent <CameraControl>(); FootstepsComponent = playerObj.GetComponent <Footsteps>(); FPSPlayerComponent = playerObj.GetComponent <FPSPlayer>(); WeaponPivotComponent = FPSPlayerComponent.WeaponPivotComponent; }
void OnTriggerExit(Collider other2) { //on exit of a collision with ladder trigger set climbing var to false on FPSRigidBodyWalker script if (other2.gameObject.tag == "Player") { FPSRigidBodyWalker FPSWalkerComponent = playerObj.GetComponent <FPSRigidBodyWalker>(); FPSWalkerComponent.climbing = false; //prevent player from jumping when leaving surface if they did so by holding jump button FPSWalkerComponent.landStartTime = Time.time + 0.25f; FPSWalkerComponent.jumpBtn = false; FPSWalkerComponent.noClimbingSfx = false; } }
void FixedUpdate() { //set up external script references FPSRigidBodyWalker FPSWalkerComponent = playerObj.GetComponent <FPSRigidBodyWalker>(); PlayerWeapons PlayerWeaponsComponent = gunObj.transform.parent.GetComponent <PlayerWeapons>(); WorldRecenter WorldRecenterComponent = playerObj.transform.GetComponent <WorldRecenter>(); if (Time.time > shellRemovalTime) { Object.Destroy(lerpShell.gameObject); Object.Destroy(gameObject); } //Check if the player is on a moving platform to determine how to handle shell parenting and velocity //if(playerObjTransform.parent == FPSWalkerComponent.mainObj.transform){//if player is not on a moving platform if ((WorldRecenterComponent.removePrefabRoot && playerObjTransform.parent == null) || (!WorldRecenterComponent.removePrefabRoot && playerObjTransform.parent == FPSWalkerComponent.mainObj.transform)) { //Make the shell's parent the weapon object for a short time after ejection //to the link shell ejection position with weapon object for more consistent movement, if (((startTime + 0.35f < Time.time && parentState) //don't parent shell if switching weapon || (PlayerWeaponsComponent.switching && parentState) //don't parent shell if moving weapon to sprinting position || (FPSWalkerComponent.sprintActive && !FPSWalkerComponent.cancelSprint && parentState)) && FPSWalkerComponent.grounded) { Vector3 tempVelocity = playerObjTransform.GetComponent <Rigidbody>().velocity; tempVelocity.y = 0.0f; myTransform.parent = null; lerpShell.parent = null; //add player velocity to shell when unparenting from player object to prevent shell from suddenly changing direction if (!FPSWalkerComponent.sprintActive && !FPSWalkerComponent.canRun) //don't inherit parent velocity if sprinting to prevent visual glitches { GetComponent <Rigidbody>().AddForce(tempVelocity, ForceMode.VelocityChange); } parentState = false; } } else //if player is on elevator, keep gun object as parent for a longer time to prevent strange shell movements { if (startTime + 0.5f < Time.time && parentState) { myTransform.parent = null; lerpShell.parent = null; //add player velocity to shell when unparenting from player object to prevent shell from suddenly changing direction GetComponent <Rigidbody>().AddForce(playerObjTransform.GetComponent <Rigidbody>().velocity, ForceMode.VelocityChange); } } }
void Start() { playerObj = transform.gameObject; FPSWalkerComponent = playerObj.GetComponent <FPSRigidBodyWalker>(); aSource = playerObj.AddComponent <AudioSource>(); aSource.spatialBlend = 0.0f; aSource.playOnAwake = false; if (Terrain.activeTerrain) //get information of active terrain { terrain = Terrain.activeTerrain; terrainData = terrain.terrainData; terrainPos = terrain.transform.position; } }
void OnEnable() { myTransform = transform; if (muzzleFlash) { muzzleFlashTransform = muzzleFlash.transform; } AIComponent = myTransform.GetComponent <AI>(); WeaponEffectsComponent = AIComponent.playerObj.GetComponent <FPSPlayer>().weaponObj.GetComponent <WeaponEffects>(); playerObj = Camera.main.transform.GetComponent <CameraControl>().playerObj; FPSWalker = playerObj.GetComponent <FPSRigidBodyWalker>(); aSource = GetComponent <AudioSource>(); //bulletsLeft = bulletsPerClip; shootStartTime = -fireRate * 2; }
void OnTriggerStay(Collider other) { if (other.gameObject.tag == "Player") { FPSRigidBodyWalker FPSWalkerComponent = playerObj.GetComponent <FPSRigidBodyWalker>(); //apply upward velocity to player rigidbody to vault ledge playerObj.GetComponent <Rigidbody>().AddForce(new Vector3(0, upwardPullForce, 0), ForceMode.VelocityChange); //set grounded in FPSRigidBodyWalker to true to allow the player //full air manipulation to move forward over ledge FPSWalkerComponent.climbing = true; FPSWalkerComponent.noClimbingSfx = true; FPSWalkerComponent.inputY = 1; //make player play bob cycle when climbing ledge FPSWalkerComponent.grounded = true; FPSWalkerComponent.jumpBtn = false; //prevent player from jumping once they reach top of ledge } }
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 OnTriggerExit(Collider col) { //set up external script references FPSRigidBodyWalker FPSWalkerComponent = playerObj.GetComponent <FPSRigidBodyWalker>(); //player has exited water, so reset swimming related variables if (col.gameObject.tag == "Player") { swimTimeState = false; rippleEffect.emit = false; particlesEffect.emit = false; FPSWalkerComponent.inWater = false; FPSWalkerComponent.swimming = false; FPSWalkerComponent.belowWater = false; FPSWalkerComponent.canWaterJump = true; FPSWalkerComponent.holdingBreath = false; } }
void OnTriggerEnter(Collider col) { FPSRigidBodyWalker FPSWalkerComponent = playerObj.GetComponent <FPSRigidBodyWalker>(); Footsteps FootstepsComponent = playerObj.GetComponent <Footsteps>(); string colTag = col.gameObject.tag; Transform colTransform = col.gameObject.transform; //play splash effects for player and objects thrown into water if ((colTag == "Player" && (FPSWalkerComponent.jumping || !FPSWalkerComponent.grounded)) || //play splash effects for player if they jumped into water ((colTag == "Usable" || //play splash effects for objects if they hit water colTag == "Metal" || colTag == "Wood" || colTag == "Glass" || col.gameObject.name == "Chest") && colTransform.position.y > myTransform.collider.bounds.max.y - 0.3f)) { if (colTag == "Player") { EnterWater(col); } AudioSource.PlayClipAtPoint(FootstepsComponent.waterLand, colTransform.position); //play splash sound if (waterSplash) { foreach (Transform child in waterSplash.transform) //emit all particles in the particle effect game object group stored in impactObj var { if (child.name == "FastSplash") { splashPos = new Vector3(colTransform.position.x, myTransform.collider.bounds.max.y - 0.15f, colTransform.position.z); child.particleEmitter.transform.position = splashPos; } else { splashPos = new Vector3(colTransform.position.x, myTransform.collider.bounds.max.y + 0.01f, colTransform.position.z); child.particleEmitter.transform.position = splashPos; } child.particleEmitter.transform.rotation = Quaternion.FromToRotation(Vector3.up, waterSplash.transform.up); //rotate impact effects so they are perpendicular to surface hit child.particleEmitter.Emit(); //emit the particle(s) } } } }
void Start() { //set up component references if (CinemaCameraObj) { CinemaCameraObj.SetActive(false); } CameraControlComponent = MainCameraObj.GetComponent <CameraControl>(); MouseLookComponent = FPSCameraObj.GetComponent <SmoothMouseLook>(); FPSPlayerComponent = FPSPlayerObj.GetComponent <FPSPlayer>(); FPSWalkerComponent = FPSPlayerObj.GetComponent <FPSRigidBodyWalker>(); PlayerWeaponsComponent = FPSWeaponsObj.GetComponent <PlayerWeapons>(); IronsightsComponent = FPSPlayerObj.GetComponent <Ironsights>(); VisibleBodyComponent = FPSWalkerComponent.VisibleBody.GetComponent <VisibleBody>(); if (!FPSPlayerComponent.crosshairEnabled) { noCrosshair = true; //don't reactivate crosshair if it wasn't active to start } }
void Start() { GunSwayComponent = transform.parent.transform.GetComponent <GunSway>(); SmoothMouseLookComponent = GunSwayComponent.cameraObj.GetComponent <SmoothMouseLook>(); playerObj = Camera.main.transform.GetComponent <CameraControl>().playerObj; FPSPlayerComponent = playerObj.GetComponent <FPSPlayer>(); FPSWalkerComponent = playerObj.GetComponent <FPSRigidBodyWalker>(); InputComponent = playerObj.GetComponent <InputControl>(); PlayerWeaponsComponent = FPSPlayerComponent.PlayerWeaponsComponent; IronsightsComponent = FPSPlayerComponent.IronsightsComponent; PivotAnimComponent = GetComponent <Animation>(); myTransform = transform; childTransform = myTransform.GetChild(0); swayAmt = GunSwayComponent.swayAmount; if (deadzoneLooking) { SmoothMouseLookComponent.dzAiming = true; } }
void Start() { CameraControlComponent = Camera.main.transform.GetComponent <CameraControl>(); playerObj = CameraControlComponent.playerObj; weaponObj = CameraControlComponent.weaponObj; playerTransform = playerObj.transform; InputComponent = playerObj.GetComponent <InputControl>(); FPSPlayerComponent = playerObj.GetComponent <FPSPlayer>(); walkerComponent = playerObj.GetComponent <FPSRigidBodyWalker>(); GunSwayComponent = weaponObj.GetComponent <GunSway>(); SmoothMouseLookComponent = GunSwayComponent.cameraObj.GetComponent <SmoothMouseLook>(); AnimationComponent = objectWithAnims.GetComponent <Animation>(); AnimationComponent.wrapMode = WrapMode.Loop; AnimationComponentShadow.wrapMode = WrapMode.Loop; myTransform = transform; verticalPos = playerTransform.position.y - modelUpAmt; tempPosX = playerTransform.position.x; tempPosZ = playerTransform.position.z; meshState1 = false; meshState2 = false; fpSkinnedMesh.gameObject.SetActive(true); }
void OnTriggerStay(Collider other) { if (other.gameObject.tag == "Player") { FPSRigidBodyWalker FPSWalkerComponent = playerObj.GetComponent <FPSRigidBodyWalker>(); FPSPlayer FPSPlayerComponent = playerObj.GetComponent <FPSPlayer>(); //on start of a collision with ladder trigger set climbing var to true on FPSRigidBodyWalker script FPSWalkerComponent.climbing = true; if (!playClimbingAudio) { FPSWalkerComponent.noClimbingSfx = true; } //dont play climbing sounds if playClimbingAudio is false if (Mathf.Abs(FPSWalkerComponent.inputY) < 0.1f) { if (Input.GetKey(FPSPlayerComponent.crouch)) { //prevent player from crouching when leaving surface if they did so by holding crouch button FPSWalkerComponent.crouchState = false; } } } }
public void InitializeShell() { //set up external script references FPSPlayerComponent = Camera.main.transform.GetComponent <CameraControl>().FPSPlayerComponent; PlayerWeaponsComponent = FPSPlayerComponent.PlayerWeaponsComponent; FPSWalkerComponent = FPSPlayerComponent.FPSWalkerComponent; myTransform = transform;//manually set transform for efficiency FPSWalkerComponent = FPSPlayerComponent.FPSWalkerComponent; //shell states and settings parentState = true; soundState = true; //initialize shell rotation amounts shellRotateUp = WeaponBehaviorComponent.shellRotateUp; shellRotateSide = WeaponBehaviorComponent.shellRotateSide; shellDuration = WeaponBehaviorComponent.shellDuration; //track the time that the shell was ejected startTime = Time.time; shellRemovalTime = Time.time + shellDuration; //time that shell will be removed RigidbodyComponent.maxAngularVelocity = 100; //allow shells to spin faster than default //determine if shell rotates clockwise or counter-clockwise at random if (Random.value < 0.5f) { shellRotateUp *= -1; } RigidbodyComponent.velocity = Vector3.zero; RigidbodyComponent.angularVelocity = Vector3.zero; //rotate shell rotateAmt = 0.1f; //apply torque to rigidbody RigidbodyComponent.AddRelativeTorque(Vector3.up * (Random.Range(0.175f, rotateAmt) * shellRotateSide), ForceMode.Impulse); RigidbodyComponent.AddRelativeTorque(Vector3.right * (Random.Range(0.4f, rotateAmt * 6) * shellRotateUp), ForceMode.Impulse); StartCoroutine(CalcShellPos()); }
bool CanSeeTarget() { FPSRigidBodyWalker FPSWalker = playerObj.GetComponent <FPSRigidBodyWalker>(); if (FPSWalker.crouched) { attackRangeAmt = attackRange * sneakRangeMod; //reduce NPC's attack range by sneakRangeMod amount when player is crouched } else { attackRangeAmt = attackRange; } if (Vector3.Distance(myTransform.position, target.position) > attackRangeAmt) { return(false); } RaycastHit hit; if (Physics.Linecast(myTransform.position + myTransform.up * (1.0f + eyeHeight), target.position, out hit, searchMask)) { return(hit.transform == target); } return(false); }
void Start() { //set up component references if (CinemaCameraObj) { CinemaCameraObj.SetActive(false); } CameraControlComponent = MainCameraObj.GetComponent <CameraControl>(); FPSPlayerObj = CameraControlComponent.playerObj; FPSWeaponsObj = CameraControlComponent.weaponObj; FPSCameraObj = CameraControlComponent.transform.parent.transform.gameObject; MainCameraObj = Camera.main.transform.gameObject; MouseLookComponent = FPSCameraObj.GetComponent <SmoothMouseLook>(); FPSPlayerComponent = FPSPlayerObj.GetComponent <FPSPlayer>(); FPSWalkerComponent = FPSPlayerObj.GetComponent <FPSRigidBodyWalker>(); PlayerWeaponsComponent = FPSWeaponsObj.GetComponent <PlayerWeapons>(); IronsightsComponent = FPSPlayerObj.GetComponent <Ironsights>(); PlayerCharacterComponent = CameraControlComponent.PlayerCharacterComponent; PlayerCharacterObj = CameraControlComponent.PlayerCharacterObj; if (!FPSPlayerComponent.crosshairEnabled) { noCrosshair = true; //don't reactivate crosshair if it wasn't active to start } }
void Update() { //Set up external script references FPSRigidBodyWalker FPSWalkerComponent = playerObj.GetComponent <FPSRigidBodyWalker>(); Ironsights IronsightsComponent = playerObj.GetComponent <Ironsights>(); FPSPlayer FPSPlayerComponent = playerObj.GetComponent <FPSPlayer>(); CameraKick CameraKickComponent = Camera.main.GetComponent <CameraKick>(); Footsteps FootstepsComponent = playerObj.GetComponent <Footsteps>(); if (Time.timeScale > 0 && Time.deltaTime > 0) //allow pausing by setting timescale to 0 { waveslice = 0.0f; if (!FPSPlayerComponent.useAxisInput) { horizontal = FPSWalkerComponent.inputX; //get input from player movement script vertical = FPSWalkerComponent.inputY; } else { horizontal = FPSWalkerComponent.inputXSmoothed; //get input from player movement script vertical = FPSWalkerComponent.inputYSmoothed; } midpoint = FPSWalkerComponent.midPos; //Start bob from view position set in player movement script if (Mathf.Abs(horizontal) != 0 || Mathf.Abs(vertical) != 0 && FPSWalkerComponent.grounded) //Perform bob only when moving and grounded { waveslice = Mathf.Sin(timer); wavesliceRoll = Mathf.Sin(timerRoll); if (Mathf.Abs(FPSWalkerComponent.inputY) > 0.1f) { inputSpeed = Mathf.Abs(FPSWalkerComponent.inputY); } else { inputSpeed = Mathf.Abs(FPSWalkerComponent.inputX); } timer = timer + bobbingSpeed * inputSpeed * Time.deltaTime; timerRoll = timerRoll + (bobbingSpeed / 2.0f) * Time.deltaTime; //Make view roll bob half the speed of view pitch bob if (timer > Mathf.PI * 2.0f) { timer = timer - (Mathf.PI * 2.0f); if (!FPSWalkerComponent.noClimbingSfx) //dont play climbing footsteps if noClimbingSfx is true { FootstepsComponent.FootstepSfx(); //play footstep sound effect by calling FootstepSfx() function in Footsteps.cs } } //Perform bobbing of camera roll if (timerRoll > Mathf.PI * 2.0f) { timerRoll = (timerRoll - (Mathf.PI * 2.0f)); if (!FPSWalkerComponent.grounded) { timerRoll = 0; //reset timer when airborne to allow soonest resume of footstep sfx } } } else { //reset variables to prevent view twitching when falling timer = 0.0f; timerRoll = 0.0f; tempLocalEulerAngles = new Vector3(0, 0, 0); //reset camera angles to 0 when stationary } if (waveslice != 0) { translateChange = waveslice * bobbingAmount; translateChangePitch = waveslice * pitchingAmount; translateChangeRoll = wavesliceRoll * rollingAmount; translateChangeYaw = wavesliceRoll * yawingAmount; totalAxes = Mathf.Abs(horizontal) + Mathf.Abs(vertical); totalAxes = Mathf.Clamp(totalAxes, 0.0f, 1.0f); //needed for smooth return to neutral view pos translateChange = totalAxes * translateChange; translateChangePitch = totalAxes * translateChangePitch; translateChangeRoll = totalAxes * translateChangeRoll; //Set position for smoothing function and add jump value //divide position by deltaTime for framerate independence dampTo = midpoint + (translateChange / Time.deltaTime * 0.01f); //camera roll and pitch bob tempLocalEulerAngles = new Vector3(translateChangePitch, translateChangeYaw, translateChangeRoll); } else { if (!FPSWalkerComponent.swimming) { if (IronsightsComponent.cameraIdleBob) { idleYBob = Mathf.Sin(Time.time * 1.25f) * 0.015f; } } else { if (IronsightsComponent.cameraSwimBob) { idleYBob = Mathf.Sin(Time.time * 1.65f) * 0.08f; //increase vertical bob when swimming } } //reset variables to prevent view twitching when falling dampTo = midpoint + idleYBob; //add small sine bob for camera idle movement totalAxes = 0; translateChange = 0; } //use SmoothDamp to smooth position and remove any small glitches in bob amount dampOrg = Mathf.SmoothDamp(dampOrg, dampTo, ref dampVelocity, 0.1f, Mathf.Infinity, Time.deltaTime); //Pass bobbing amount and angles to the camera kick script in the camera object after smoothing CameraKickComponent.dampOriginY = dampOrg; CameraKickComponent.bobAngles = Vector3.SmoothDamp(CameraKickComponent.bobAngles, tempLocalEulerAngles, ref dampVelocity2, 0.1f, Mathf.Infinity, Time.deltaTime); } }