Advertisement
infinite_ammo

Mover.cs

Jan 25th, 2015
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.85 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4.  
  5. public class Mover : StateMonoBehaviour
  6. {
  7.     protected int checkLayerMask;
  8.     protected bool moving, turning;
  9.     protected float turnAngle;
  10.  
  11.     void Awake()
  12.     {
  13.         if (checkLayerMask == 0)
  14.             checkLayerMask = LayerMask.GetMask("Obstruction");
  15.     }
  16.  
  17.     protected Collider MoveCheck(Vector3 move)
  18.     {
  19.         float distance = move.magnitude;
  20.         Vector3 dir = move.normalized;
  21.         var hits = rigidbody.SweepTestAll(dir, distance);
  22.         Collider collider = null;
  23.         foreach (var hit in hits)
  24.         {
  25.             collider = hit.collider;
  26.             break;
  27.         }
  28.         return collider;
  29.     }
  30.  
  31.     protected bool Move(Vector3 move, Action<Collider> onHit)
  32.     {
  33.         var collider = MoveCheck(move);
  34.  
  35.         if (collider == null)
  36.         {
  37.             transform.position += move;
  38.         }
  39.         else
  40.         {
  41.             if (onHit != null)
  42.                 onHit(collider);
  43.         }
  44.  
  45.         return collider != null;
  46.     }
  47.  
  48.     protected bool MoveXZ(Vector3 move, Action<Collider> onHit)
  49.     {
  50.         var collider = MoveCheck(move);
  51.         if (collider != null)
  52.             onHit(collider);
  53.  
  54.         if (collider == null)
  55.             transform.position += move;
  56.  
  57.         return collider != null;
  58.     }
  59.  
  60.     protected bool SlideMoveXZ(Vector3 move, Action<Collider> onHit)
  61.     {
  62.         var collider = MoveCheck(move);
  63.         if (collider != null)
  64.         {
  65.             onHit(collider);
  66.             var oldMove = move;
  67.             if (Mathf.Abs(move.x) > Mathf.Abs(move.z))
  68.             {
  69.                 move.z = 0f;
  70.                 collider = MoveCheck(move);
  71.                 if (collider != null)
  72.                 {
  73.                     move = oldMove;
  74.                     move.x = 0f;
  75.                     collider = MoveCheck(move);
  76.                 }
  77.             }
  78.             else
  79.             {
  80.                 move.x = 0f;
  81.                 collider = MoveCheck(move);
  82.                 if (collider != null)
  83.                 {
  84.                     move = oldMove;
  85.                     move.z = 0f;
  86.                     collider = MoveCheck(move);
  87.                 }
  88.             }
  89.         }
  90.  
  91.         if (collider == null)
  92.             transform.position += move;
  93.  
  94.         return collider != null;
  95.     }
  96.  
  97.     protected bool IsGroundAt(Vector3 point)
  98.     {
  99.         return IsGroundAt(point, Vector3.down);
  100.     }
  101.  
  102.     protected bool IsGroundAt(Vector3 point, Vector3 down)
  103.     {
  104.         return Physics.CheckSphere(point + down, .25f, checkLayerMask);
  105.     }
  106.  
  107.     protected bool IsBlockedAt(Vector3 point)
  108.     {
  109.         return Physics.CheckSphere(point, .25f, checkLayerMask);
  110.     }
  111.  
  112.     protected bool CanMoveToTile(Vector3 point)
  113.     {
  114.         return !IsBlockedAt(point) && IsGroundAt(point);
  115.     }
  116.    
  117.     protected IEnumerator Crawl(Vector3 dir, float speed, Transform pivot, float turnSpeed)
  118.     {
  119.         Debug.LogWarning("Crawl!");
  120.         if (IsGroundAt(pivot.position + pivot.up * .5f, -pivot.up))
  121.         {
  122.             bool blockedAhead = IsBlockedAt(pivot.position + pivot.up * .5f + pivot.forward * .5f);
  123.             bool groundAhead = IsGroundAt(pivot.position + pivot.up * .5f + pivot.forward * .5f, -pivot.up);
  124.             Debug.LogWarning("blockedAhead: " + blockedAhead + " groundAhead: " + groundAhead);
  125.             if (!blockedAhead && groundAhead)
  126.             {
  127.                 yield return StartCoroutine(DoCrawl(pivot.forward/2f, speed));
  128.  
  129.                 if (!IsGroundAt(pivot.position + pivot.forward/2f + pivot.up * .5f))
  130.                 {
  131.                     yield return StartCoroutine(DoTurnToOpenCrawlSpace(pivot, turnSpeed, .5f, 1f));
  132.                 }
  133.                 else
  134.                 {
  135.                     yield return StartCoroutine(DoCrawl(pivot.forward/2f, speed));
  136.                 }
  137.             }
  138.             else if (blockedAhead)
  139.             {
  140.                 if (!IsBlockedAt(pivot.position + pivot.up * .5f))
  141.                 {
  142.                     yield return StartCoroutine(DoCrawl(pivot.forward/2f, speed));
  143.                 }
  144.                 Debug.LogWarning("turn 1");
  145.                 yield return StartCoroutine(DoTurnToOpenCrawlSpace(pivot, turnSpeed, .5f, -1f));
  146.             }
  147.             else if (!groundAhead)
  148.             {
  149.                 Debug.LogWarning("turn 2");
  150.                 yield return StartCoroutine(DoTurnToOpenCrawlSpace(pivot, turnSpeed, .5f, 1f));
  151.             }
  152.         }
  153.         else
  154.         {
  155.             Debug.LogWarning("Stuck");
  156.             yield return null;
  157.         }
  158.     }
  159.  
  160.     protected IEnumerator DoTurnToOpenCrawlSpace(Transform pivot, float speed, float dist, float turnDir = 1f, Tween.EaseType easeType = Tween.EaseType.Linear)
  161.     {
  162.         turning = true;
  163.         float duration = 1f/speed;
  164.  
  165.        
  166.         while (IsBlockedAt(pivot.position + pivot.up * .5f + pivot.forward * dist) || !IsGroundAt(pivot.position + pivot.up * .5f + pivot.forward * dist, -pivot.up))
  167.         {
  168.             bool blockedAhead = IsBlockedAt(pivot.position + pivot.up * .5f + pivot.forward * dist);
  169.             bool groundAhead = IsGroundAt(pivot.position + pivot.up * .5f + pivot.forward * dist, -pivot.up);
  170.             Debug.LogWarning("blockedAhead: " + blockedAhead + " groundAhead: " + groundAhead);
  171.  
  172.             var targetRotation = Quaternion.EulerAngles(turnAngle + turnDir * 90f, 0f, 0f);
  173.             var startLocalRotation = pivot.localRotation;
  174.             float startAngle = turnAngle;
  175.             float endAngle = startAngle + 90f * turnDir;
  176.             float timer = 0f;
  177.             while (timer < duration)
  178.             {
  179.                
  180.                 /*
  181.                 timer += Time.deltaTime;
  182.                 if (timer > duration)
  183.                     timer = duration;
  184.                 float p = timer/duration;
  185.                 pivot.localRotation = Quaternion.Slerp(startLocalRotation, targetRotation, p);
  186.                
  187.                 yield return null;
  188.                 */
  189.  
  190.  
  191.                 timer += Time.deltaTime;
  192.                 if (timer > duration)
  193.                     timer = duration;
  194.                 float p = timer/duration;
  195.                 pivot.localRotation = Quaternion.Euler(startAngle * (1f - p) + endAngle * p, 0f, 0f);
  196.                 //Vector3 localEulerAngles = pivot.localEulerAngles;
  197.                 // = startAngle * (1f - p) + endAngle * p;
  198.                 //pivot.localEulerAngles = localEulerAngles;
  199.                 yield return null;
  200.             }
  201.             //yield return new WaitForSeconds(.1f);
  202.             turnAngle += turnDir * 90f;
  203.  
  204.             //var targetRotation = Quaternion.Slerp(pivot.localRotation, Quaternion.EulerAngles(pivot.localEulerAngles + Vector3.right * 90f * turnDir), 360f);
  205.             //Tween.RotateTo(pivot.gameObject, pivot.localEulerAngles + Vector3.right * 90f * turnDir, time, easeType, true);
  206.             //yield return new WaitForSeconds(time);
  207.         }
  208.        
  209.         turning = false;
  210.  
  211.         yield return null;
  212.     }
  213.  
  214.     protected IEnumerator DoCrawl(Vector3 move, float speed)
  215.     {
  216.         moving = true;
  217.         float time = move.magnitude / speed;
  218.         Tween.MoveTo(gameObject, transform.position + move, time, Tween.EaseType.Linear, true);
  219.         yield return new WaitForSeconds(time);
  220.         moving = false;
  221.     }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement