protected void Hanging(float elapsedTime) { VerticalSpeed = 0; if (MecanimAnimator.GetBool(MecanimHashes.Hang)) { if (ActiveHangTarget == null) { MecanimAnimator.SetBool(MecanimHashes.Fall, true); return; } if (ActiveHangTarget.DoesFaceZAxis()) { HorizontalSpeed = 0.0f; Direction = Vector3.zero; } else { HorizontalSpeed = Direction.x * Settings.LedgeClimbingSpeed; if (IsHangTargetToRight) { Direction = Vector3.right; } else { Direction = Vector3.left; } } MecanimAnimator.SetBool(MecanimHashes.Hang, false); } if (ActiveHangTarget != null) { ActivePlatform = ActiveHangTarget.transform; } MecanimAnimator.SetBool(MecanimHashes.Fall, false); if (ActiveHangTarget == null) { DropHangTarget(); MecanimAnimator.SetBool(MecanimHashes.Fall, true); } else if (ActiveHangTarget is Ledge && (CharInput.Up || InputMoveForward)) { _ledge = ActiveHangTarget as Ledge; MecanimAnimator.SetBool(MecanimHashes.ClimbLedge, true); } else if (CharInput.JumpPressed) { DropHangTarget(); MecanimAnimator.SetBool(MecanimHashes.Jump, true); } else if (CharInput.Down) { DropHangTarget(); MecanimAnimator.SetBool(MecanimHashes.Fall, true); } }
// States on the ground from which you can jump should call this method to set up the jump private void AllowGroundJump() { if (!MecanimAnimator.GetBool(MecanimHashes.Jump) && IsGrounded && CharInput.JumpPressed) { if (CharInput.JumpLeft) { Direction = Vector3.left; float jumpSpeedRatio = -1.1f; if (IsSneaking) { jumpSpeedRatio = -0.79f; } HorizontalSpeed = jumpSpeedRatio * Settings.MaxHorizontalSpeed; } else if (CharInput.JumpRight) { Direction = Vector3.right; float jumpSpeedRatio = 1.1f; if (IsSneaking) { jumpSpeedRatio = 0.79f; } HorizontalSpeed = jumpSpeedRatio * Settings.MaxHorizontalSpeed; } SetMecanimAnimatorHorizontalSpeedFloat(); MecanimAnimator.SetBool(MecanimHashes.Jump, true); } }
// RollingState = Animator.StringToHash("Ground.Rolling"); protected void Rolling(float elapsedTime) { // We only need to set motion settings at the beginning of the animation if (!MecanimAnimator.GetBool(MecanimHashes.ClimbLedge)) { return; } // We need to make sure we know what we're going to roll over. if (ActiveHangTarget == null) { Debug.LogWarning("Failed to have hang target while doing rolling!"); return; } // Meta-data about the motion we need to do MecanimAnimator.SetBool(MecanimHashes.ClimbLedge, false); Bounds ledgeBounds = ActiveHangTarget.GetComponent <Collider>().bounds; float animationTime = MecanimAnimator.GetCurrentAnimatorStateInfo(0).length; // Horizontal motion float distanceToMove = Direction.x > 0 ? ledgeBounds.max.x - Controller.bounds.center.x : ledgeBounds.min.x - Controller.bounds.center.x; HorizontalSpeed = distanceToMove / animationTime; SetMecanimAnimatorHorizontalSpeedFloat(); // Vertical motion float distanceToClimb = ledgeBounds.max.y - Controller.bounds.min.y; VerticalSpeed = distanceToClimb / animationTime; MecanimAnimator.SetFloat(MecanimHashes.VerticalSpeed, VerticalSpeed); }
protected void StealthDeath(float elapsedTime) { if (MecanimAnimator.GetBool(MecanimHashes.StealthDeath)) { MecanimAnimator.SetBool(MecanimHashes.StealthDeath, false); } }
protected virtual void Idle(float elapsedTime) { if (IgnoreXYMovement) { return; } // Turn on the screens when he begines to idle if (TimeInCurrentState == 0) { IdleScreenAnimation.Play("Take 001"); foreach (Fader screen in IdleScreens) { screen.FadeIn(); } } ApplyMovingHorizontal(elapsedTime); VerticalSpeed = GroundVerticalSpeed; ApplyBiDirection(); if (!MecanimAnimator.GetBool(MecanimHashes.Fall) && !IsGrounded) { MecanimAnimator.SetBool(MecanimHashes.Fall, true); } if (CharInput.Pickup) { MecanimAnimator.SetBool(MecanimHashes.TurnAround, true); } }
protected override void UpdateMecanimVariables() { if (IgnoreXYMovement) { return; } if (!MecanimAnimator.GetBool(MecanimHashes.Jump) && IsGrounded && CharInput.JumpPressed) { MecanimAnimator.SetBool(MecanimHashes.Jump, true); } bool facingRightLadder = (ActiveHangTarget && ActiveHangTarget.transform.position.x - transform.position.x > 0.0f); bool facingLeftLadder = (ActiveHangTarget && transform.position.x - ActiveHangTarget.transform.position.x > 0.0f); bool startClimbLadder = CanClimbLadder && ((facingRightLadder && CharInput.Right) || (facingLeftLadder && CharInput.Left)); MecanimAnimator.SetBool(MecanimHashes.ClimbLadder, startClimbLadder); /* * if(startClimbLadder) * _autoClimbDir = AutoClimbDirection.AutoClimb_Up; * // if not in a climb, reset our auto-climb direction for use next climb. * if( (CurrentState.fullPathHash != ClimbingLadderState || CurrentState.fullPathHash != ClimbingPipeState) ) * _autoClimbDir = AutoClimbDirection.AutoClimb_None; */ MecanimAnimator.SetBool(MecanimHashes.ClimbRope, CanClimbRope); MecanimAnimator.SetBool(MecanimHashes.IsGrounded, IsGrounded); MecanimAnimator.SetBool(MecanimHashes.AttackHorizontal, CharInput.Attack > 0); MecanimAnimator.SetBool(MecanimHashes.AttackVertical, CharInput.Attack < 0); if (!IsGrounded) { MecanimAnimator.SetBool(MecanimHashes.AcquiringTarget, false); } // Give Olympus some perfectly hard stops on land if (IsLanding) { HorizontalSpeed = 0; } // Knowing direction is useful for the turnaround animation MecanimAnimator.SetFloat(MecanimHashes.XDirection, Direction.x); // Olympus needs to turn off the screens when moving // TODO: THINK OF A MORE EFFICIENT WAY INSTEAD OF DOING THIS EVERY FRAME if (CharInput.Left || CharInput.Right) { foreach (Fader screen in IdleScreens) { screen.FadeOut(); } } }
protected void GrabWall(float elapsedTime) { if (MecanimAnimator.GetBool(MecanimHashes.GrabWall)) { HorizontalSpeed = 0; VerticalSpeed = 0; MecanimAnimator.SetBool(MecanimHashes.GrabWall, false); } }
// JumpingState = Animator.StringToHash("Jumping.Jumping"); // FrontflipState = Animator.StringToHash("Jumping.Frontflip"); // FrontflipLoopState = Animator.StringToHash("Jumping.FrontflipLoop"); protected void Jumping(float elapsedTime) { // We only determine vertical speed (Horizontal Speed is maintained throughout the jump) if (MecanimAnimator.GetBool(MecanimHashes.Jump)) { float jumpHeight = Settings.JumpHeight; if (CurrentState.fullPathHash == FrontflipState) { jumpHeight *= 1.5f; } VerticalSpeed = Mathf.Sqrt(2 * jumpHeight * Settings.Gravity); MecanimAnimator.SetFloat(MecanimHashes.VerticalSpeed, VerticalSpeed); MecanimAnimator.SetBool(MecanimHashes.Jump, false); } else { ApplyGravity(elapsedTime); } // You can increase horizontal speed midair ONLY if doing a sneak jump and you weren't already at max speed if (CurrentState.fullPathHash == JumpingState && (CharInput.Left || CharInput.Right)) { float prevSpeed = HorizontalSpeed; ApplyMovingHorizontal(elapsedTime * 0.5f); if (Mathf.Abs(HorizontalSpeed) < Mathf.Abs(prevSpeed)) { HorizontalSpeed = prevSpeed; } else if (Direction.x > 0 && HorizontalSpeed > 0.79f * Settings.MaxHorizontalSpeed) { HorizontalSpeed = 0.79f * Settings.MaxHorizontalSpeed; } else if (Direction.x < 0 && HorizontalSpeed < -0.79f * Settings.MaxHorizontalSpeed) { HorizontalSpeed = -0.79f * Settings.MaxHorizontalSpeed; } SetMecanimAnimatorHorizontalSpeedFloat(); } // You can grab onto walls while jumping MecanimAnimator.SetBool(MecanimHashes.GrabWall, CanGrabWall); // You can hang off most objects while jumping bool shouldHang = (CanHangOffObject && ActiveHangTarget.DoesFaceXAxis() && VerticalSpeed < 0) || (CanHangOffObject && ActiveHangTarget.DoesFaceZAxis() && CharInput.Up); MecanimAnimator.SetBool(MecanimHashes.Hang, shouldHang); // You can re-grab ropes you just let go of bool shouldRegrabRope = CharInput.Up && PreviousHangTarget != null && PreviousHangTarget.GetComponent <Collider>().bounds.Contains(transform.position); if (shouldRegrabRope) { AddHangTarget(PreviousHangTarget); MecanimAnimator.SetBool(MecanimHashes.ClimbRope, true); } }
protected void TurnAround(float elapsedTime) { if (MecanimAnimator.GetBool(MecanimHashes.TurnAround)) { MecanimAnimator.SetBool(MecanimHashes.TurnAround, false); StartCoroutine(WaitToChangeDirection()); } HorizontalSpeed = 0; VerticalSpeed = GroundVerticalSpeed; }
protected void Running(float elapsedTime) { ApplyMovingHorizontal(elapsedTime); VerticalSpeed = GroundVerticalSpeed; ApplyBiDirection(); if (!MecanimAnimator.GetBool(MecanimHashes.Fall) && !IsGrounded) { MecanimAnimator.SetBool(MecanimHashes.Fall, true); } }
protected void Searching(float elapsedTime) { if (MecanimAnimator.GetBool(MecanimHashes.Search)) { HorizontalSpeed = 0; VerticalSpeed = GroundVerticalSpeed; MecanimAnimator.SetBool(MecanimHashes.Search, false); } if (!MecanimAnimator.GetBool(MecanimHashes.Fall) && !IsGrounded) { MecanimAnimator.SetBool(MecanimHashes.Fall, true); } }
// DamagedState = Animator.StringToHash("Combat.Damaged"); protected void Damaged(float elapsedTime) { if (MecanimAnimator.GetBool(MecanimHashes.Damaged)) { MecanimAnimator.SetBool(MecanimHashes.Damaged, false); GameManager.UI.EnableInput(); GameManager.UI.CraftingMenu.Close(); GameManager.UI.ShowMap(false); } ApplyGravity(elapsedTime); if (IsGrounded) { ApplyFriction(elapsedTime); } }
// WalljumpingState = Animator.StringToHash("Wall.Walljumping"); protected void Walljumping(float elapsedTime) { // Determine motion on the first frame if (MecanimAnimator.GetBool(MecanimHashes.JumpWall)) { Direction = -Direction; HorizontalSpeed = Settings.MaxHorizontalSpeed * Direction.x * 0.79f; SetMecanimAnimatorHorizontalSpeedFloat(); VerticalSpeed = Mathf.Sqrt(2 * Settings.JumpHeight * Settings.Gravity); MecanimAnimator.SetFloat(MecanimHashes.VerticalSpeed, VerticalSpeed); _wallJumpCount++; MecanimAnimator.SetBool(MecanimHashes.JumpWall, false); } else { ApplyGravity(elapsedTime); } // You eventually start falling down if (transform.position.y >= LastGroundHeight - 1) { MecanimAnimator.SetBool(MecanimHashes.Fall, false); } // You can do consecutive wall grabs/jumps if (CanGrabWall) { MecanimAnimator.SetBool(MecanimHashes.GrabWall, true); } // You can grab onto ledges if (!(ActiveHangTarget is Ledge && ((Ledge)ActiveHangTarget).Obstacle) && (CanHangOffObject && ActiveHangTarget.DoesFaceXAxis() && VerticalSpeed < 0) || (CanHangOffObject && ActiveHangTarget.DoesFaceZAxis() && CharInput.Up)) { MecanimAnimator.SetBool(MecanimHashes.Hang, true); VerticalSpeed = 0; MecanimAnimator.SetFloat(MecanimHashes.VerticalSpeed, VerticalSpeed); HorizontalSpeed = 0; } else { MecanimAnimator.SetBool(MecanimHashes.Hang, false); } }
protected void Jumping(float elapsedTime) { if (Mathf.Abs(CharInput.Horizontal) > 0.1) { ApplyMovingHorizontal(elapsedTime); } if (MecanimAnimator.GetBool(MecanimHashes.Jump)) { if (CharInput.JumpLeft || CharInput.JumpLeftReleased) { Direction = Vector3.left; HorizontalSpeed = -1.0f * Settings.MaxHorizontalSpeed; } else if (CharInput.JumpRight || CharInput.JumpRightReleased) { Direction = Vector3.right; HorizontalSpeed = 1.0f * Settings.MaxHorizontalSpeed; } VerticalSpeed = Mathf.Sqrt(2 * Settings.JumpHeight * Settings.Gravity); MecanimAnimator.SetBool(MecanimHashes.Jump, false); } else { ApplyGravity(elapsedTime); } //ApplyBiDirection(); if (transform.position.y >= LastGroundHeight - 1) { MecanimAnimator.SetBool(MecanimHashes.Fall, false); } MecanimAnimator.SetBool(MecanimHashes.Hang, (CanHangOffObject && ActiveHangTarget.DoesFaceXAxis() && VerticalSpeed < 0) || (CanHangOffObject && ActiveHangTarget.DoesFaceZAxis() && CharInput.Up)); if (CanGrabWall && ((Direction.x > 0 && IsHangTargetToRight) || (Direction.x < 0 && !IsHangTargetToRight))) { MecanimAnimator.SetBool(MecanimHashes.GrabWall, true); } }
// PickupState = Animator.StringToHash("Kneeling.Pickup"); protected void Pickup(float elapsedTime) { // Everything relevant happens on the first frame if (MecanimAnimator.GetBool(MecanimHashes.Pickup)) { MecanimAnimator.SetBool(MecanimHashes.Pickup, false); // Horizontal Motion HorizontalSpeed = 0; SetMecanimAnimatorHorizontalSpeedFloat(); // Vertical Motion VerticalSpeed = GroundVerticalSpeed; MecanimAnimator.SetFloat(MecanimHashes.VerticalSpeed, VerticalSpeed); // Pickup a weapon if (_itemPickedup.WeaponPrefab != null) { int pickupCount = _itemPickedup.Quantity; Weapon pickedUpWeapon = _itemPickedup.WeaponPrefab.GetComponent <Weapon>(); if (!GameManager.Inventory.TryAddAmmo(pickedUpWeapon, pickupCount)) { // NOTE: DON'T USE THE OBJECT POOL BECAUSE IT DOESN'T REALLY HAPPEN THAT OFTEN Transform instantiatedWeapon = (Transform)Instantiate(_itemPickedup.WeaponPrefab); Weapon newWeapon = instantiatedWeapon.GetComponent <Weapon>(); newWeapon.Quantity = pickupCount; GameManager.Inventory.Weapons.Add(newWeapon); GameManager.UI.CycleToNextWeapon(); } GameManager.UI.RefreshWeaponWheel(); // Pickup an inventory item } else { InventoryItem newInvItem = InventoryItemFactory.CreateFromType(_itemPickedup.Type, _itemPickedup.Quantity); GameManager.Inventory.AddItem(newInvItem); GameManager.UI.CraftingMenu.RefreshItemWheel(); } // Get rid of the picked up item Destroy(_itemPickedup.gameObject, 0.5f); } }
// BackflipState = Animator.StringToHash("Air.Backflip"); protected void Backflip(float elapsedTime) { // We determine our target speed on the first frame of the action if (MecanimAnimator.GetBool(MecanimHashes.Backflip)) { MecanimAnimator.SetBool(MecanimHashes.Backflip, false); _desiredDirectionX = -Direction.x; _desiredSpeed = -HorizontalSpeed; VerticalSpeed = Mathf.Sqrt(2 * Settings.JumpHeight * 1.5f * Settings.Gravity); MecanimAnimator.SetFloat(MecanimHashes.VerticalSpeed, VerticalSpeed); } else { ApplyGravity(elapsedTime); } // Adjust horizontal speed frame by frame float accelerationSmoothing = Settings.HorizontalAcceleration * elapsedTime; if (TimeInCurrentState > 0.15f) { Vector3 newDir = new Vector3(Mathf.Lerp(Direction.x, _desiredDirectionX, accelerationSmoothing), 0, 0); if (newDir.x * Direction.x < 0) { HorizontalSpeed = -HorizontalSpeed; } Direction = newDir; } accelerationSmoothing = Settings.HorizontalAcceleration * elapsedTime; HorizontalSpeed = Mathf.Lerp(HorizontalSpeed, _desiredSpeed, accelerationSmoothing); SetMecanimAnimatorHorizontalSpeedFloat(); // You can grab onto objects even in the middle of a backflip bool shouldHang = (CanHangOffObject && ActiveHangTarget.DoesFaceXAxis() && VerticalSpeed < 0) || (CanHangOffObject && ActiveHangTarget.DoesFaceZAxis() && CharInput.Up); MecanimAnimator.SetBool(MecanimHashes.Hang, shouldHang); // Make sure we don't enter the other Air states MecanimAnimator.SetBool(MecanimHashes.Jump, false); MecanimAnimator.SetBool(MecanimHashes.Fall, false); }
protected void Attack(float elapsedTime) { if (!MecanimAnimator.GetBool(MecanimHashes.Attack)) { return; } // Set up mecanim MecanimAnimator.SetBool(MecanimHashes.Attack, false); // Disable movement Controller.enabled = false; IgnoreMovement = true; IgnoreDirection = true; // Parent ourselves to the player transform.parent = GameManager.Player.transform; Vector3 move = transform.localPosition; transform.localPosition = Vector3.zero; Settings.RootTransform.position = Settings.RootTransform.position + move; }
// HangingState = Animator.StringToHash("Climbing.Hanging"); protected void Hanging(float elapsedTime) { // Make sure we actually have something to hang on if (ActiveHangTarget == null) { DropHangTarget(); MecanimAnimator.SetBool(MecanimHashes.Fall, true); Debug.LogWarning("Player lost track of hang target and fell!"); return; } // We determine all our motion on the first frame if (MecanimAnimator.GetBool(MecanimHashes.Hang)) { if (ActiveHangTarget.DoesFaceZAxis()) { HorizontalSpeed = 0.0f; Direction = Vector3.zero; } else { HorizontalSpeed = Direction.x * Settings.LedgeClimbingSpeed; if (IsHangTargetToRight) { Direction = Vector3.right; } else { Direction = Vector3.left; } } SetMecanimAnimatorHorizontalSpeedFloat(); VerticalSpeed = 0; MecanimAnimator.SetFloat(MecanimHashes.VerticalSpeed, VerticalSpeed); MecanimAnimator.SetBool(MecanimHashes.Hang, false); } // Stay on whatever we're hanging on, even if it's moving ActivePlatform = ActiveHangTarget.transform; MecanimAnimator.SetBool(MecanimHashes.Fall, false); // You can press up/forward on ledges to climb up if (ActiveHangTarget is Ledge && (CharInput.Up || InputMoveForward)) { _ledge = ActiveHangTarget as Ledge; MecanimAnimator.SetBool(MecanimHashes.ClimbLedge, true); // You can jump off whatever you're hanging } else if (CharInput.JumpPressed) { AllowAirJump(); // You can also just let go } else if (CharInput.PickupPressed) { DropHangTarget(); MecanimAnimator.SetBool(MecanimHashes.Fall, true); } }