Advertisement
ZeronSix

Cover Template

Jul 11th, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.00 KB | None | 0 0
  1. #region Cover Stuff
  2.     void CoverManagement()
  3.     {
  4.         float h = Input.GetAxis("Horizontal");
  5.         bool coverPressed = Input.GetButtonDown("Cover");
  6.         int state; // current animator
  7.  
  8.         #region check for covers
  9.         state = anim.GetCurrentAnimatorStateInfo(0).nameHash;
  10.         if (coverPressed)
  11.         {
  12.             if (!inCover)
  13.             {
  14.                 coverPosition = CheckForCovers(out coverRotation);
  15.                 if (coverPosition != Vector3.zero)
  16.                 {
  17.                     inCover = true;
  18.                     (collider as CapsuleCollider).radius = coverColliderRadius;
  19.                     (collider as CapsuleCollider).height = 2.01f;
  20.                 }
  21.                 else
  22.                 {
  23.                     coverRotation = Quaternion.identity;
  24.                     inCover = false;
  25.                 }
  26.  
  27.                 anim.SetBool(hash.coverBool, inCover);
  28.             }
  29.             else if (!(state == hash.coverMoveLeftState || state == hash.coverMoveRightState))
  30.             {
  31.                 // get out from cover
  32.                 inCover = false;
  33.                 (collider as CapsuleCollider).radius = baseColliderRadius;
  34.                 (collider as CapsuleCollider).height = 2f;
  35.                 anim.SetBool(hash.coverBool, inCover);
  36.             }
  37.         }
  38.  
  39.         state = anim.GetCurrentAnimatorStateInfo(0).nameHash;
  40.  
  41.         if (state == hash.inCoverState)
  42.         {
  43.             anim.MatchTarget(coverPosition, coverRotation, AvatarTarget.Root,
  44.                              new MatchTargetWeightMask(new Vector3(1, 0, 1), 1), 0f);
  45.         }
  46.         #endregion
  47.         #region movement
  48.         RaycastHit hit;
  49.         state = anim.GetCurrentAnimatorStateInfo(0).nameHash;
  50.  
  51.         float m = 1f;   // multiplier for the vector
  52.         if (h < 0) m = -1f;
  53.  
  54.         if (h != 0)
  55.         {
  56.             bool raycast = Physics.Raycast(transform.position + Vector3.up + (transform.TransformDirection(Vector3.left) * m),
  57.                                     transform.TransformDirection(Vector3.back), out hit, 1f);
  58.  
  59.             if (raycast && hit.transform.gameObject.tag == Tags.environment)
  60.                 anim.SetFloat(hash.horizontalFloat, h);
  61.             else anim.SetFloat(hash.horizontalFloat, 0);
  62.         }
  63.  
  64.         Physics.Raycast(transform.position + Vector3.up, transform.TransformDirection(Vector3.back), out hit, 1f);
  65.         if (state == hash.coverIdleState || state == hash.coverMoveLeftState
  66.                 || state == hash.coverMoveRightState && !anim.IsInTransition(0))
  67.             rigidbody.MoveRotation(Quaternion.LookRotation(hit.normal, Vector3.up));
  68.  
  69.         #endregion
  70.     }
  71.  
  72.     Vector3 CheckForCovers(out Quaternion rotation)
  73.     {
  74.         // check for covers in all 4 directions
  75.         RaycastHit hit;
  76.         rotation = Quaternion.identity;
  77.  
  78.         if (RaycastToWall(transform.TransformDirection(Vector3.forward), out hit))
  79.         {
  80.             rotation = Quaternion.LookRotation(hit.normal, Vector3.up);
  81.             return hit.point;
  82.         }
  83.  
  84.         if (RaycastToWall(transform.TransformDirection(Vector3.left), out hit))
  85.         {
  86.             rotation = Quaternion.LookRotation(hit.normal, Vector3.up);
  87.             return hit.point;
  88.         }
  89.  
  90.         if (RaycastToWall(transform.TransformDirection(Vector3.right), out hit))
  91.         {
  92.             rotation = Quaternion.LookRotation(hit.normal, Vector3.up);
  93.             return hit.point;
  94.         }
  95.        
  96.         // return zero vector if nothing happens
  97.         return Vector3.zero;
  98.     }
  99.  
  100.     bool RaycastToWall(Vector3 direction, out RaycastHit raycastHit)
  101.     {
  102.         bool raycast = Physics.Raycast(transform.position + Vector3.up, direction, out raycastHit, 2f);
  103.  
  104.         if (raycast)
  105.         {
  106.             // check if that object is environment
  107.             if (raycastHit.transform.gameObject.tag == Tags.environment)
  108.             {
  109.                 return true;
  110.             }
  111.         }
  112.  
  113.         return false;
  114.     }
  115.     #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement