void HandleSprinting() { if (Input.GetButtonDown("Sprint")) { if (myState == State.Standard || myCrouchState == CrouchState.Crouching) { if (myCrouchState == CrouchState.Crouching) { myCrouchState = CrouchState.Standing; playerSpeed = 455; maxSpeed = 3; } myState = State.Sprinting; //playerSpeed = 655; //maxSpeed = 6; } else { myState = State.Standard; //playerSpeed = 455; //maxSpeed = 3; } } if ((myState == State.Sprinting && movementIntensity < 0.9f) || myCrouchState == CrouchState.Crouching) { myState = State.Standard; } }
private void UpdatePreviousStates() { previousJumpState = currentJumpState; previouslyWasStrafing = currentlyIsStrafing; previousCrouchState = currentCrouchState; previousRunState = currentRunState; }
public PlayerState(IEventManager playerEvent) { #region Player State Initialization _StateIdle = new IdleState(this); _StateWalk = new WalkState(this); _StateSprint = new SprintState(this); _StateFall = new FallState(this); _StateCrouch = new CrouchState(this); _StateJump = new JumpState(this); _StateCrouchWalk = new CrouchWalkState(this); _StateSlide = new SlideState(this); _StateCrouchJump = new CrouchJumpState(this); _StateCrouchFall = new CrouchFallState(this); _StateAirMoveCrouchFall = new AirMoveCrouchFallState(this); _StateAirMoveCrouchJump = new AirMoveCrouchJumpState(this); _StateAirMoveFall = new AirMoveFallState(this); _StateAirMoveJump = new AirMoveJumpState(this); #endregion _ActiveProxies = new HashSet <IDynamicProxy>(); _EventPlayer = playerEvent; _CurrentPlayerState = _StateIdle; #region State Proxy Initialization _MediatorToggleProxy = new ToggleProxyMediator(); _ProxySprint = new SprintProxy(this, _MediatorToggleProxy); _ProxyCrouch = new CrouchProxy(this, _MediatorToggleProxy); _MediatorToggleProxy.ProxyCrouch = _ProxyCrouch; _MediatorToggleProxy.ProxySprint = _ProxySprint; _ProxyWalk = new WalkProxy(this, _MediatorToggleProxy); _ProxyFall = new FallProxy(this); _ProxyJump = new JumpProxy(this, _MediatorToggleProxy); #endregion }
public void Excute(CharacterMove t) { t.MoveDir = Vector3.zero; if (t.isCrouchingEnd) { t.stateMachine.ChangeState(CrouchState.GetInstance()); } }
public bool ToggleCrouch() { if (currentCrouchState == CrouchState.Standing) { currentCrouchState = CrouchState.Crouched; } else { currentCrouchState = CrouchState.Standing; } return(IsCrouching()); }
public void SetCrouchState(bool crouched, bool instant = false) { isCrouched = crouched; if (isCrouched == false) { //Check if uncrouching is possible if (isCrouched == false) { //Set height and center of collider my_collider.height = 2; my_collider.center = new Vector3 { x = 0f, y = 0f, z = 0f }; crouch_state = CrouchState.Rising; } } else { //Set height of collider my_collider.height = 1; my_collider.center = new Vector3 { x = 0f, y = -0.5f, z = 0f }; crouch_state = CrouchState.Falling; } if (instant) { Vector3 target = Vector3.zero; switch (crouch_state) { case CrouchState.Rising: target = new Vector3(0f, 0.5f, 0f); crouch_state = CrouchState.NotCrouched; break; case CrouchState.Falling: target = new Vector3(0f, -0.1f, 0f); crouch_state = CrouchState.Crouched; break; } my_camera.transform.localPosition = target; } }
// Use this for initialization public override void OnStartLocalPlayer() { accelRate += frictionCoefficient; // do this so that the friction doesn't cancel out the given acceleration rate characterController = GetComponent <CharacterController>(); height = characterController.height; radius = characterController.radius; previousJumpState = JumpState.Grounded; currentJumpState = JumpState.Grounded; previousCrouchState = CrouchState.Standing; currentCrouchState = CrouchState.Standing; previousRunState = RunState.Still; currentRunState = RunState.Still; previouslyWasStrafing = false; currentlyIsStrafing = false; jumpCoefficient = Mathf.Sqrt(-1 * Physics.gravity.y * jumpHeight * 2); // velocity required to reach jumpHeight }
public override void OnEnter() { if (!UpdateCacheAndTransform(Fsm.GetOwnerDefaultTarget(gameObject))) { Finish(); return; } originalHeight = controller.height; crouchState = CrouchState.stand; transitionTimeElapsed = 0; childOffsets.Clear(); foreach (Transform child in cachedTransform) { childOffsets.Add(child, child.localPosition.y); } }
public void LocalPlayerUpdateCrouchState(ref CrouchState incoming, ref bool crouchFlag, ref bool crouchBlockFlag, ref Smoothing smoothing) { double initialEyesOffsetY = base.initialEyesOffsetY; double num2 = incoming.BottomY + initialEyesOffsetY; double num3 = incoming.BottomY + incoming.InitialStandingHeight; double num4 = num3 - num2; double num5 = incoming.TopY - num4; double num6 = num5 - incoming.BottomY; double target = num6 - initialEyesOffsetY; this.crouchUnits = smoothing.CatchUp(target); base.idMain.InvalidateEyesOffset(); if (incoming.CrouchBlocked) { crouchBlockFlag = true; crouchFlag = true; } else { crouchBlockFlag = false; } }
void HandleCrouching() { Vector3 targetCamPos = myCamera.transform.InverseTransformDirection(cameraPositions[(int)myCrouchState]); if (Input.GetButtonDown("Crouch")) { if (myCrouchState == CrouchState.Standing || myState == State.Sprinting) { myCrouchState = CrouchState.Crouching; playerSpeed = 255; maxSpeed = 1.5f; } else if (myCrouchState == CrouchState.Crouching) { myCrouchState = CrouchState.Standing; playerSpeed = 455; maxSpeed = 3; } } targetCamPos -= Vector3.up * curBobAmount; myCamera.transform.localPosition = Vector3.Lerp(myCamera.transform.localPosition, targetCamPos, 4 * Time.deltaTime); }
private void Awake() { // 创建状态机 StateMachine = new StateMachine(); // 创建所有的状态 var idleState = new IdleState(StateID.Idle, this); var runState = new RunState(StateID.Run, this); var jumpState = new JumpState(StateID.Jump, this); var fallState = new FallState(StateID.Fall, this); var hurtState = new HurtState(StateID.Hurt, this); var crouchState = new CrouchState(StateID.Crouch, this); var deathState = new DeathState(StateID.Death, this); // 初始化状态字典 StateMachine.AddState(idleState); StateMachine.AddState(runState); StateMachine.AddState(jumpState); StateMachine.AddState(fallState); StateMachine.AddState(hurtState); StateMachine.AddState(crouchState); StateMachine.AddState(deathState); // 初始化状态机 StateMachine.Initialize(idleState); }
private void Start() { controller = gameObject.GetComponent <CharacterController>(); initialFov = theCamera.fieldOfView; initialControllerHeight = controller.height; initialControllerOffset = controller.center; _cameraInitialPos = theCamera.transform.localPosition; stateMachine = new StateMachine(); idleState = new IdleState(this, stateMachine); walkState = new WalkState(this, stateMachine); runState = new RunState(this, stateMachine); jumpState = new JumpState(this, stateMachine); crouchState = new CrouchState(this, stateMachine); crouchWalkState = new CrouchWalkState(this, stateMachine); peekState = new PeekState(this, stateMachine); zoomState = new ZoomState(this, stateMachine); lookBackState = new LookBackState(this, stateMachine); //more states here stateMachine.Initialize(idleState); }
/// <summary> /// Initialize controlling state machine /// </summary> private void InitializeStateMachine() { Debug.Log("Init state machine"); // States IdleState idleState = new IdleState(); idleState.Parent = this; mStateMachine.AddState(idleState); SkiState skiState = new SkiState(); skiState.Parent = this; mStateMachine.AddState(skiState); CrouchState crouchState = new CrouchState(); crouchState.Parent = this; mStateMachine.AddState(crouchState); JumpState jumpState = new JumpState(); jumpState.Parent = this; mStateMachine.AddState(jumpState); InAirState inAirState = new InAirState(); inAirState.Parent = this; mStateMachine.AddState(inAirState); LandingState landState = new LandingState(); landState.Parent = this; mStateMachine.AddState(landState); FallDownState fallDownState = new FallDownState(); fallDownState.Parent = this; mStateMachine.AddState(fallDownState); // Transitions /////////////////// // Grounded states mStateMachine.AddTransition( CharacterEvents.To_Ski, idleState, skiState); mStateMachine.AddTransition( CharacterEvents.To_Idle, skiState, idleState); // Jump / Land states mStateMachine.AddTransition( CharacterEvents.To_Crouch, skiState, crouchState); mStateMachine.AddTransition( CharacterEvents.To_Jump, crouchState, jumpState); mStateMachine.AddTransition( CharacterEvents.To_InAir, jumpState, inAirState); mStateMachine.AddTransition( CharacterEvents.To_Landing, inAirState, landState); mStateMachine.AddTransition( CharacterEvents.To_Ski, landState, skiState); // Ramp states mStateMachine.AddTransition( CharacterEvents.To_InAir, skiState, inAirState); // for initialization mStateMachine.AddTransition( CharacterEvents.To_InAir, idleState, inAirState); // for fall down mStateMachine.AddTransition( CharacterEvents.To_FallDown, idleState, fallDownState); mStateMachine.AddTransition( CharacterEvents.To_FallDown, skiState, fallDownState); mStateMachine.AddTransition( CharacterEvents.To_FallDown, crouchState, fallDownState); // character starts in a spawn in state mStateMachine.StartState = idleState; mStateMachine.Activate(null); DeactivateRagdoll(); }
public override void OnUpdate() { if (!UpdateCacheAndTransform(Fsm.GetOwnerDefaultTarget(gameObject))) { Finish(); return; } float newHeight; switch (crouchState) { case CrouchState.stand: if (isCrouching.Value) { crouchState = CrouchState.standToCrouch; startTransitionHeight = controller.height; transitionTimeElapsed = 0; } break; case CrouchState.standToCrouch: if (transitionTimeElapsed < transitionTime.Value) { newHeight = Mathf.Lerp(startTransitionHeight, crouchHeight.Value, transitionTimeElapsed / transitionTime.Value); transitionTimeElapsed += Time.deltaTime; } else { newHeight = crouchHeight.Value; crouchState = CrouchState.crouch; } SetHeight(newHeight); if (!completeTransition.Value && !isCrouching.Value) { crouchState = CrouchState.crouchToStand; startTransitionHeight = controller.height; transitionTimeElapsed = 0; } break; case CrouchState.crouch: if (canStand.Value && (!isCrouching.Value || standToggle.Value)) { crouchState = CrouchState.crouchToStand; startTransitionHeight = controller.height; transitionTimeElapsed = 0; } break; case CrouchState.crouchToStand: if (transitionTimeElapsed < transitionTime.Value) { newHeight = Mathf.Lerp(startTransitionHeight, originalHeight, transitionTimeElapsed / transitionTime.Value); transitionTimeElapsed += Time.deltaTime; } else { newHeight = originalHeight; crouchState = CrouchState.stand; } SetHeight(newHeight); if (crouchState == CrouchState.stand) { Fsm.Event(standEvent); } break; } }
private void FixedUpdate() { Collider2D standingOn = groundDown.DoRaycast(transform.position); bool grounded = standingOn != null; if (grounded && lastGrounded == false) { audioManager.fx.PlayOneShot(landFX, 0.25f); } lastGrounded = grounded; switch (jumpState) { case JumpState.None: /*if(grounded && jumpStartTimer > 0) { * jumpStartTimer = 0; * jumpState = JumpState.Holding; * jumpHoldTimer = 0; * velocity.y = jumpStartSpeed; * jumpSFX.Play(); * }*/ if (!stunned) { if (jumpStartTimer > 0) { jumpStartTimer = 0; jumpState = JumpState.Holding; jumpHoldTimer = 0; velocity.y = jumpStartSpeed; //jumpSFX.Play(); } } break; case JumpState.Holding: jumpHoldTimer += Time.deltaTime; if (jumpInputDown == false || jumpHoldTimer >= jumpMaxHoldPeriod) { jumpState = JumpState.None; velocity.y = Mathf.Lerp(jumpMinSpeed, jumpStartSpeed, jumpHoldTimer / jumpMaxHoldPeriod); // Lerp! //float p = jumpHoldTimer / jumpMaxHoldPeriod; //velocity.y = jumpMinSpeed + (jumpStartSpeed - jumpMinSpeed) * p; } break; } switch (dashState) { case DashState.None: if (!stunned) { if (dashStartTimer > 0) { dashStartTimer = 0; dashState = DashState.Holding; dashHoldTimer = 0; //dashFX.Play(); } } break; case DashState.Holding: dashHoldTimer += Time.deltaTime; if (dashInputDown == false || dashHoldTimer >= dashMaxHoldPeriod) { dashState = DashState.None; } break; } switch (boostState) { case BoostState.None: if (!stunned) { if (boostStartTimer > 0) { boostStartTimer = 0; boostState = BoostState.Holding; boostHoldTimer = 0; //boostFX.Play(); } } break; case BoostState.Holding: boostHoldTimer += Time.deltaTime; if (boostInputDown == false || boostHoldTimer >= boostMaxHoldPeriod) { boostState = BoostState.None; } break; } switch (eraseState) { case EraseState.None: if (!stunned) { if (eraseStartTimer > 0) { eraseStartTimer = 0; eraseState = EraseState.Holding; erasehHoldTimer = 0; //eraseFX.Play(); } } break; case EraseState.Holding: erasehHoldTimer += Time.deltaTime; if (eraseInputDown == false || erasehHoldTimer >= eraseMaxHoldPeriod) { eraseState = EraseState.None; } break; } switch (crouchState) { case CrouchState.None: if (grounded && !stunned) { if (crouchStartTimer > 0) { crouchStartTimer = 0; crouchState = CrouchState.Holding; crouchHoldTimer = 0; boxCollider.enabled = false; } if (dropPlatform != null) { dropPlatform.enabled = true; } } break; case CrouchState.Holding: crouchHoldTimer += Time.deltaTime; dropPlatform = lastStandingOn; if (dropPlatform.gameObject.tag == "Droppable") { if (crouchInputDown == false || stunned) { crouchState = CrouchState.None; boxCollider.enabled = true; } else if (crouchHoldTimer >= crouchDropPeriod) { crouchState = CrouchState.None; boxCollider.enabled = true; lastStandingOn.enabled = false; } } else { if (crouchInputDown == false || stunned) { crouchState = CrouchState.None; boxCollider.enabled = true; } } break; } float horizInput = Input.GetAxisRaw("Horizontal"); int wantedDirection = GetSign(horizInput); int velocityDirection = GetSign(velocity.x); if (wantedDirection != 0) { if (wantedDirection != velocityDirection) { if (boost <= 0) { velocity.x = horizSnapSpeed * wantedDirection; //audioManager.fx.PlayOneShot(startMoveFX, 0.3f); } else { if (dashState == DashState.Holding) { dash += (int)dashSpeed; if (dash < 32) { if (!spriteRenderer.flipX) { velocity.x = dash; } else if (spriteRenderer.flipX) { velocity.x = -dash; } } else { velocity.x = Mathf.MoveTowards(velocity.x, 0, 75 * Time.deltaTime); } } else { velocity.x = 0; } } } else { if (!stunned) { if (dashState == DashState.Holding) { dash += (int)dashSpeed; if (dash < 32) { if (!spriteRenderer.flipX) { velocity.x = dash; } else if (spriteRenderer.flipX) { velocity.x = -dash; } } else { velocity.x = Mathf.MoveTowards(velocity.x, 0, 75 * Time.deltaTime); } } else if (boostState == BoostState.Holding) { boost += (int)boostSpeed; velocity.x = 0; if (boost <= 36) { velocity.y = boost; } else { velocity.y = Mathf.MoveTowards(velocity.y, 0, 125 * Time.deltaTime); } } else { if (boost <= 0) { dash = 0; boost = 0; velocity.x = Mathf.MoveTowards(velocity.x, horizMaxSpeed * wantedDirection, horizSpeedUpAccel * Time.deltaTime); } else { velocity.x = 0; velocity.y = Mathf.MoveTowards(velocity.y, 0, 200 * Time.deltaTime); } } } } } else { if (dashState == DashState.Holding) { dash += (int)dashSpeed; if (dash < 32) { if (!spriteRenderer.flipX) { velocity.x = dash; } else if (spriteRenderer.flipX) { velocity.x = -dash; } } else { velocity.x = Mathf.MoveTowards(velocity.x, 0, 75 * Time.deltaTime); } } else if (boostState == BoostState.Holding) { boost += (int)boostSpeed; velocity.x = 0; if (boost <= 36) { velocity.y = boost; } else { velocity.y = Mathf.MoveTowards(velocity.y, 0, 125 * Time.deltaTime); } } else { if (dash > 0) { dash -= (int)dashSpeed; velocity.x = Mathf.MoveTowards(velocity.x, 0, 150 * Time.deltaTime); } else { if (boost > 0) { velocity.x = 0; velocity.y = Mathf.MoveTowards(velocity.y, 0, 200 * Time.deltaTime); } else { dash = 0; boost = 0; velocity.x = Mathf.MoveTowards(velocity.x, 0, horizSpeedDownAccel * Time.deltaTime); } } } } if (dashState != DashState.Holding) { if (jumpState == JumpState.None) { if (boostState != BoostState.Holding) { if (eraseState != EraseState.Holding) { if (boost > 0) { boost -= (int)boostSpeed / 2; velocity.y -= gravity * Time.deltaTime; velocity.x = 0; } else { velocity.y -= gravity * Time.deltaTime; if (crouchState == CrouchState.Holding) { velocity.x = 0; } } } else { velocity.x = 0; velocity.y = 0; } } else if (boostState == BoostState.Holding) { velocity.x = 0; stunned = false; } } if (stunned) { if (currentX != 0) { velocity.x = (currentX * -1) * stunBounce; stunTimer += Time.deltaTime; } else { velocity.x = (enemyX * -3) * stunBounce; stunTimer += Time.deltaTime; } if (stunTimer >= stunPeriod) { stunned = false; velocity.x = 0; } } else { currentX = velocity.x; stunTimer = 0; } } else if (dashState == DashState.Holding) { velocity.y += vertBoost * Time.deltaTime; stunned = false; } Vector2 displacement = Vector2.zero; Vector2 wantedDispl = velocity * Time.deltaTime; if (standingOn != null) { if (lastStandingOn == standingOn) { lastStandingOnVel = (Vector2)standingOn.transform.position - lastStandingOnPos; wantedDispl += lastStandingOnVel; } else if (standingOn == null) { velocity += lastStandingOnVel / Time.deltaTime; wantedDispl += lastStandingOnVel; } lastStandingOnPos = standingOn.transform.position; } lastStandingOn = standingOn; if (wantedDispl.x > 0) { displacement.x = moveRight.DoRaycast(transform.position, wantedDispl.x); } else if (wantedDispl.x < 0) { displacement.x = -moveLeft.DoRaycast(transform.position, -wantedDispl.x); } if (wantedDispl.y > 0) { displacement.y = moveUp.DoRaycast(transform.position, wantedDispl.y); } else if (wantedDispl.y < 0) { displacement.y = -moveDown.DoRaycast(transform.position, -wantedDispl.y); } if (Mathf.Approximately(displacement.x, wantedDispl.x) == false) { velocity.x = 0; } if (Mathf.Approximately(displacement.y, wantedDispl.y) == false) { velocity.y = 0; } transform.Translate(displacement); if (!stunned) { if (dashState == DashState.Holding) { if (playerAnim.cycleCounter != 4) { playerAnim.flip = true; playerAnim.cycleCounter = 4; } } else { if (jumpState == JumpState.Holding) { if (playerAnim.cycleCounter != 3) { playerAnim.flip = true; playerAnim.cycleCounter = 3; } } else { if (eraseState == EraseState.Holding) { if (playerAnim.cycleCounter != 8) { playerAnim.flip = true; playerAnim.cycleCounter = 8; } } else { if (grounded) { if (wantedDirection == 0) { if (crouchState == CrouchState.Holding) { if (playerAnim.cycleCounter != 6) { playerAnim.flip = true; playerAnim.cycleCounter = 6; } } else { if (playerAnim.cycleCounter != 0) { playerAnim.flip = true; playerAnim.cycleCounter = 0; } } } else { if (playerAnim.cycleCounter != 1) { playerAnim.flip = true; playerAnim.cycleCounter = 1; } } } else { if (velocity.y < 0) { if (playerAnim.cycleCounter != 2) { playerAnim.flip = true; playerAnim.cycleCounter = 2; } } else { if (boostState == BoostState.Holding) { if (playerAnim.cycleCounter != 5) { playerAnim.flip = true; playerAnim.cycleCounter = 5; } } } } } } } } else if (stunned) { if (playerAnim.cycleCounter != 7) { playerAnim.flip = true; playerAnim.cycleCounter = 7; } } if (wantedDirection != 0) { spriteRenderer.flipX = wantedDirection < 0; blankSR.flipX = wantedDirection < 0; } }
// Update is called once per frame private void Update() { bool grounded = checkGrounded(); //Get jump input if (grounded) { if (Input.GetButtonDown("Jump")) { was_jump_pressed = true; } } else { if (highestY < transform.position.y) { highestY = transform.position.y; Debug.DrawRay(transform.position, Vector3.up, Color.blue, 10f); } } //Get Sprint desired_speed = (Input.GetButton("Sprint")) ? (sprint_speed) : (walk_speed); /* * //Get Falling * is_feather_falling = (my_rigidbody.velocity.y < 0 && Input.GetButton("Jump")); */ //Get Move Input move_vector.z = Input.GetAxis("Vertical") * desired_speed * Time.fixedDeltaTime; move_vector.x = Input.GetAxis("Horizontal") * desired_speed * Time.fixedDeltaTime; move_vector = transform.localRotation * move_vector; move_vector = Vector3.Lerp(prev_move_vector, move_vector, 0.2f); move_vector = Vector3.ClampMagnitude(move_vector, desired_speed * Time.fixedDeltaTime); //Get Crouch Input if (Input.GetButtonDown("Crouch")) { if (isCrouched) { if (!Physics.CheckSphere(transform.position + new Vector3(0f, 0.5f, 0f), my_collider.radius - 0.1f, layer_mask_solid)) { SetCrouchState(false); } } else { SetCrouchState(true); } } //Lerp the camera based on crouch if (crouch_state != CrouchState.Crouched && crouch_state != CrouchState.NotCrouched) { Vector3 target = Vector3.zero; switch (crouch_state) { case CrouchState.Rising: target = new Vector3(0f, 0.5f, 0f); break; case CrouchState.Falling: target = new Vector3(0f, -0.1f, 0f); break; } my_camera.transform.localPosition = Vector3.Lerp(my_camera.transform.localPosition, target, 0.1f); if (my_camera.transform.localPosition == target) { switch (crouch_state) { case CrouchState.Rising: crouch_state = CrouchState.NotCrouched; break; case CrouchState.Falling: crouch_state = CrouchState.Crouched; break; } } } //Locking the cursor if (Input.GetKeyDown("escape")) { //Toggle cursor lock Cursor.lockState = (Cursor.lockState == CursorLockMode.Locked) ? (CursorLockMode.None) : (CursorLockMode.Locked); Cursor.visible = (Cursor.lockState == CursorLockMode.None); } //Restarting scene if (Input.GetKeyDown(KeyCode.F1)) { //Force character to spwan like they do in the editor firstSpawnInScene = false; SceneManager.LoadScene(SceneManager.GetActiveScene().name); } if (Input.GetKeyDown(KeyCode.F2)) { dealDamage(25); } //Update health bar healthBar.fillAmount = Mathf.Lerp(healthBar.fillAmount, health / 100f, 0.1f); healthText.text = health.ToString(); }
// Update is called once per frame private void Update() { grounded = checkGrounded(); //Get jump input if (grounded) { if (Input.GetButtonDown("Jump")) { JumpPressed = true; } } else { if (HighestY < transform.position.y) { HighestY = transform.position.y; Debug.DrawRay(transform.position, Vector3.up, Color.blue, 10f); } } /* * //Get Falling * is_feather_falling = (my_rigidbody.velocity.y < 0 && Input.GetButton("Jump")); */ //Get Move Input direction.z = Input.GetAxis("Vertical"); direction.x = Input.GetAxis("Horizontal"); // Set the desired direction of the player accordingly to the updated z and x axis variables direction = transform.localRotation * direction; direction = direction.normalized; //Declare and set the target speed Vector3 targetVelocity; targetVelocity = direction * (Input.GetButton("Sprint") ? (sprintSpeed) : (walkSpeed)); //Get player velocity with y component set to zero. Vector3 playerVelocityNoGravity = new Vector3(playerVelocity.x, 0, playerVelocity.z); //Determine whether or not to accelerate bool accelerate = Vector3.Dot(direction, playerVelocityNoGravity) > 0.8f; //Calculate Velocity float acceleration = grounded ? accelerationWalk : accelerationAir; float deacceleration = grounded ? deaccelerationWalk : deaccelerationAir; playerVelocity = Vector3.Lerp(playerVelocity, targetVelocity, (accelerate ? acceleration : deacceleration) * Time.deltaTime); //Get Crouch Input if (Input.GetButtonDown("Crouch")) { if (IsCrouched) { if (!Physics.CheckSphere(transform.position + new Vector3(0f, 0.5f, 0f), _Collider.radius - 0.1f, solidLayerMask)) { SetCrouchState(false); } } else { SetCrouchState(true); } } //Lerp the camera based on crouch if (_CrouchState != CrouchState.Crouched && _CrouchState != CrouchState.NotCrouched) { Vector3 target = Vector3.zero; switch (_CrouchState) { case CrouchState.Rising: target = new Vector3(0f, 0.5f, 0f); break; case CrouchState.Falling: target = new Vector3(0f, -0.1f, 0f); break; } _Camera.transform.localPosition = Vector3.Lerp(_Camera.transform.localPosition, target, 0.1f); if (_Camera.transform.localPosition == target) { switch (_CrouchState) { case CrouchState.Rising: _CrouchState = CrouchState.NotCrouched; break; case CrouchState.Falling: _CrouchState = CrouchState.Crouched; break; } } } //Locking the cursor if (Input.GetKeyDown("escape")) { TogglePause(); } //Restarting scene if (Input.GetKeyDown(KeyCode.F1)) { //Force character to spwan like they do in the editor firstSpawnInScene = false; SceneManager.LoadScene(SceneManager.GetActiveScene().name); } //Update health bar //HealthBar.fillAmount = Mathf.Lerp(HealthBar.fillAmount, Health / 100f, 0.1f); //HealthText.text = healthManager.health.ToString(); }