private CapsuleCollider col; // Reference to the player collider. // Start is always called after any Awake functions. void Start() { // Set up the references. possibleAction = CoverActions.NONE; currentAction = CoverActions.NONE; coveringBool = Animator.StringToHash("Cover"); crouchFloat = Animator.StringToHash("Crouch"); cornerBool = Animator.StringToHash("Corner"); changeCoverBool = Animator.StringToHash("ChangeCover"); jumpCoverBool = Animator.StringToHash("JumpCover"); jumpBool = Animator.StringToHash("Jump"); aimBool = Animator.StringToHash("Aim"); shift = Vector3.zero; path = new NavMeshPath(); line = this.GetComponent <LineRenderer>(); wayPointIndex = 0; col = GetComponent <CapsuleCollider> (); standingHeight = col.height; maxJumpCoverDist = col.radius * 5; coverSign = GameObject.Find("GameController").transform.GetChild(3).GetChild(0).gameObject; turnCoverSign = GameObject.Find("GameController").transform.GetChild(3).GetChild(1).gameObject; changeCoverSign = GameObject.Find("GameController").transform.GetChild(3).GetChild(2).gameObject; jumpCoverSign = GameObject.Find("GameController").transform.GetChild(3).GetChild(3).gameObject; // Subscribe this behaviour on the manager. behaviourManager.SubscribeBehaviour(this); }
// Manage cover action when cover button is pressed. private void HandleCoverActions() { switch (possibleAction) { // Change covers situation. case CoverActions.CHANGE: currentAction = CoverActions.CHANGE; UndrawSign(changeCoverSign); behaviourManager.GetAnim.SetBool(changeCoverBool, true); behaviourManager.LockTempBehaviour(this.behaviourCode); break; // Turn cover corner situation. case CoverActions.TURN: currentAction = CoverActions.TURN; break; // Jump over cover situation. case CoverActions.JUMP: currentAction = CoverActions.JUMP; behaviourManager.SetLastDirection(-coverWall.normal); behaviourManager.GetAnim.SetBool(jumpCoverBool, true); behaviourManager.LockTempBehaviour(this.behaviourCode); break; default: currentAction = CoverActions.NONE; break; } }
// Handle the jump over cover action. private void JumpCoverManagement() { // Can only jump over cover if player is crouching, cover depth is within max range, not moving, pressing forward button, and is in FOV. Vector3 castOrigin = transform.position - (maxJumpCoverDist + 0.1f + col.radius) * transform.forward + standingHeight * Vector3.up; bool canJumpCover = !Physics.Raycast(castOrigin, Vector3.down, 2.0f, layerMask); canJumpCover = canJumpCover && !Physics.Raycast(castOrigin, transform.forward, 1.5f); canJumpCover = canJumpCover && (Mathf.Abs(behaviourManager.GetH) < 0.2f); canJumpCover = canJumpCover && (behaviourManager.GetV > 0.5f) && isCrouching; canJumpCover = canJumpCover && InPLayerFOV(GetCamTarget(), true); // Draw jump cover cover sign and adjust cover end depth. if (canJumpCover && currentAction == CoverActions.NONE) { possibleAction = CoverActions.JUMP; Vector3 signPosition = transform.position + (Vector3.up * 1.5f); DrawSign(jumpCoverSign, signPosition, -coverWall.normal, 30f); jumpCoverEnd = transform.position - (AdjustCoverEnd(maxJumpCoverDist) + col.radius) * transform.forward + standingHeight * Vector3.up; Debug.DrawRay(jumpCoverEnd, Vector3.down, canJumpCover ? Color.green : Color.red); } else { UndrawSign(jumpCoverSign); if (possibleAction == CoverActions.JUMP && currentAction == CoverActions.NONE) { possibleAction = CoverActions.NONE; } } }
// End jumping over cover (called by animation). public void EndJumpOver() { possibleAction = currentAction = CoverActions.NONE; col.enabled = true; behaviourManager.GetAnim.SetBool(jumpCoverBool, false); behaviourManager.GetAnim.SetFloat(speedFloat, 0.1f); takeCover = false; EndCover(); }
// Handle the turn cover corner action. private void TurnCoverManagement() { // End turning cover when passed corner. if ((currentAction == CoverActions.TURN) && !corner) { currentAction = CoverActions.NONE; } // Can turn the cover corner only when no path to other cover is avaliable. if (!possibleCover) { // Ignore corner stop until passed the current corner. corner = corner && (currentAction != CoverActions.TURN); } }
private CapsuleCollider col; // Reference to the player collider. // Start is always called after any Awake functions. void Start() { // Set up the references. possibleAction = CoverActions.NONE; currentAction = CoverActions.NONE; layerMask = 1 << LayerMask.NameToLayer("Cover"); coveringBool = Animator.StringToHash("Cover"); crouchFloat = Animator.StringToHash("Crouch"); cornerBool = Animator.StringToHash("Corner"); changeCoverBool = Animator.StringToHash("ChangeCover"); jumpCoverBool = Animator.StringToHash("JumpCover"); aimBool = Animator.StringToHash("Aim"); shift = Vector3.zero; path = new UnityEngine.AI.NavMeshPath(); line = this.GetComponent <LineRenderer>(); wayPointIndex = 0; col = GetComponent <CapsuleCollider> (); standingHeight = col.height; maxJumpCoverDist = col.radius * 5; // Subscribe this behaviour on the manager. behaviourManager.SubscribeBehaviour(this); }
// End the change cover action. public void EndChangeCover() { currentAction = CoverActions.NONE; behaviourManager.GetAnim.SetBool(changeCoverBool, false); behaviourManager.UnlockTempBehaviour(this.behaviourCode); }
// Deal with corner moves, dead ends. private void CheckForCorners() { // Get the direction of cover movement. int direction = 0; if (behaviourManager.IsHorizontalMoving()) { direction = (int)Mathf.Sign(behaviourManager.GetH); } // Get projected position when moving (collider edge). shift.x = -coverWall.normal.z; shift.z = coverWall.normal.x; shift.y = 0; shift *= direction; shift *= col.radius; Ray ray = new Ray((castOriginHeight * Vector3.up) - (col.radius * 0.9f * transform.forward) + transform.position + shift, -coverWall.normal); // Is player on a corner? corner = !Physics.Raycast(ray.origin, ray.direction, 1.0f, layerMask); Debug.DrawRay(ray.origin, ray.direction, corner ? Color.red : Color.green); // Handle dead ends. DeadEndManagement(ray); // Handle turning cover action started on previous frames. TurnCoverManagement(); // Check if a special action is possible. RaycastHit hit; if (CanChangeCover(ray, out hit)) { possibleAction = CoverActions.CHANGE; } else if (CanTurnCover(ray, out hit)) { possibleAction = CoverActions.TURN; } else if (possibleAction != CoverActions.JUMP) { possibleAction = CoverActions.NONE; } // Draw special action sign. Vector3 signPosition = transform.position + (3f * shift) + (Vector3.up * 0.5f); switch (possibleAction) { case CoverActions.CHANGE: DrawSign(changeCoverSign, signPosition, -coverWall.normal, 90f, direction, true); UndrawSign(turnCoverSign); break; case CoverActions.TURN: DrawSign(turnCoverSign, signPosition, -coverWall.normal, 90f, direction, true); UndrawSign(changeCoverSign); break; default: UndrawSign(changeCoverSign); UndrawSign(turnCoverSign); break; } // Shift camera position on corners. int factor = ((corner && isCovering) && behaviourManager.IsHorizontalMoving()) ? 1 : 0; factor *= direction; behaviourManager.GetCamScript.SetXCamOffset(factor * camCornerOffset); // Set corner parameter on animator controller. behaviourManager.GetAnim.SetBool(cornerBool, corner); }