Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Cover Stuff
- void CoverManagement()
- {
- float h = Input.GetAxis("Horizontal");
- bool coverPressed = Input.GetButtonDown("Cover");
- int state; // current animator
- #region check for covers
- state = anim.GetCurrentAnimatorStateInfo(0).nameHash;
- if (coverPressed)
- {
- if (!inCover)
- {
- coverPosition = CheckForCovers(out coverRotation);
- if (coverPosition != Vector3.zero)
- {
- inCover = true;
- (collider as CapsuleCollider).radius = coverColliderRadius;
- (collider as CapsuleCollider).height = 2.01f;
- }
- else
- {
- coverRotation = Quaternion.identity;
- inCover = false;
- }
- anim.SetBool(hash.coverBool, inCover);
- }
- else if (!(state == hash.coverMoveLeftState || state == hash.coverMoveRightState))
- {
- // get out from cover
- inCover = false;
- (collider as CapsuleCollider).radius = baseColliderRadius;
- (collider as CapsuleCollider).height = 2f;
- anim.SetBool(hash.coverBool, inCover);
- }
- }
- state = anim.GetCurrentAnimatorStateInfo(0).nameHash;
- if (state == hash.inCoverState)
- {
- anim.MatchTarget(coverPosition, coverRotation, AvatarTarget.Root,
- new MatchTargetWeightMask(new Vector3(1, 0, 1), 1), 0f);
- }
- #endregion
- #region movement
- RaycastHit hit;
- state = anim.GetCurrentAnimatorStateInfo(0).nameHash;
- float m = 1f; // multiplier for the vector
- if (h < 0) m = -1f;
- if (h != 0)
- {
- bool raycast = Physics.Raycast(transform.position + Vector3.up + (transform.TransformDirection(Vector3.left) * m),
- transform.TransformDirection(Vector3.back), out hit, 1f);
- if (raycast && hit.transform.gameObject.tag == Tags.environment)
- anim.SetFloat(hash.horizontalFloat, h);
- else anim.SetFloat(hash.horizontalFloat, 0);
- }
- Physics.Raycast(transform.position + Vector3.up, transform.TransformDirection(Vector3.back), out hit, 1f);
- if (state == hash.coverIdleState || state == hash.coverMoveLeftState
- || state == hash.coverMoveRightState && !anim.IsInTransition(0))
- rigidbody.MoveRotation(Quaternion.LookRotation(hit.normal, Vector3.up));
- #endregion
- }
- Vector3 CheckForCovers(out Quaternion rotation)
- {
- // check for covers in all 4 directions
- RaycastHit hit;
- rotation = Quaternion.identity;
- if (RaycastToWall(transform.TransformDirection(Vector3.forward), out hit))
- {
- rotation = Quaternion.LookRotation(hit.normal, Vector3.up);
- return hit.point;
- }
- if (RaycastToWall(transform.TransformDirection(Vector3.left), out hit))
- {
- rotation = Quaternion.LookRotation(hit.normal, Vector3.up);
- return hit.point;
- }
- if (RaycastToWall(transform.TransformDirection(Vector3.right), out hit))
- {
- rotation = Quaternion.LookRotation(hit.normal, Vector3.up);
- return hit.point;
- }
- // return zero vector if nothing happens
- return Vector3.zero;
- }
- bool RaycastToWall(Vector3 direction, out RaycastHit raycastHit)
- {
- bool raycast = Physics.Raycast(transform.position + Vector3.up, direction, out raycastHit, 2f);
- if (raycast)
- {
- // check if that object is environment
- if (raycastHit.transform.gameObject.tag == Tags.environment)
- {
- return true;
- }
- }
- return false;
- }
- #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement