Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System;
- public class Mover : StateMonoBehaviour
- {
- protected int checkLayerMask;
- protected bool moving, turning;
- protected float turnAngle;
- void Awake()
- {
- if (checkLayerMask == 0)
- checkLayerMask = LayerMask.GetMask("Obstruction");
- }
- protected Collider MoveCheck(Vector3 move)
- {
- float distance = move.magnitude;
- Vector3 dir = move.normalized;
- var hits = rigidbody.SweepTestAll(dir, distance);
- Collider collider = null;
- foreach (var hit in hits)
- {
- collider = hit.collider;
- break;
- }
- return collider;
- }
- protected bool Move(Vector3 move, Action<Collider> onHit)
- {
- var collider = MoveCheck(move);
- if (collider == null)
- {
- transform.position += move;
- }
- else
- {
- if (onHit != null)
- onHit(collider);
- }
- return collider != null;
- }
- protected bool MoveXZ(Vector3 move, Action<Collider> onHit)
- {
- var collider = MoveCheck(move);
- if (collider != null)
- onHit(collider);
- if (collider == null)
- transform.position += move;
- return collider != null;
- }
- protected bool SlideMoveXZ(Vector3 move, Action<Collider> onHit)
- {
- var collider = MoveCheck(move);
- if (collider != null)
- {
- onHit(collider);
- var oldMove = move;
- if (Mathf.Abs(move.x) > Mathf.Abs(move.z))
- {
- move.z = 0f;
- collider = MoveCheck(move);
- if (collider != null)
- {
- move = oldMove;
- move.x = 0f;
- collider = MoveCheck(move);
- }
- }
- else
- {
- move.x = 0f;
- collider = MoveCheck(move);
- if (collider != null)
- {
- move = oldMove;
- move.z = 0f;
- collider = MoveCheck(move);
- }
- }
- }
- if (collider == null)
- transform.position += move;
- return collider != null;
- }
- protected bool IsGroundAt(Vector3 point)
- {
- return IsGroundAt(point, Vector3.down);
- }
- protected bool IsGroundAt(Vector3 point, Vector3 down)
- {
- return Physics.CheckSphere(point + down, .25f, checkLayerMask);
- }
- protected bool IsBlockedAt(Vector3 point)
- {
- return Physics.CheckSphere(point, .25f, checkLayerMask);
- }
- protected bool CanMoveToTile(Vector3 point)
- {
- return !IsBlockedAt(point) && IsGroundAt(point);
- }
- protected IEnumerator Crawl(Vector3 dir, float speed, Transform pivot, float turnSpeed)
- {
- Debug.LogWarning("Crawl!");
- if (IsGroundAt(pivot.position + pivot.up * .5f, -pivot.up))
- {
- bool blockedAhead = IsBlockedAt(pivot.position + pivot.up * .5f + pivot.forward * .5f);
- bool groundAhead = IsGroundAt(pivot.position + pivot.up * .5f + pivot.forward * .5f, -pivot.up);
- Debug.LogWarning("blockedAhead: " + blockedAhead + " groundAhead: " + groundAhead);
- if (!blockedAhead && groundAhead)
- {
- yield return StartCoroutine(DoCrawl(pivot.forward/2f, speed));
- if (!IsGroundAt(pivot.position + pivot.forward/2f + pivot.up * .5f))
- {
- yield return StartCoroutine(DoTurnToOpenCrawlSpace(pivot, turnSpeed, .5f, 1f));
- }
- else
- {
- yield return StartCoroutine(DoCrawl(pivot.forward/2f, speed));
- }
- }
- else if (blockedAhead)
- {
- if (!IsBlockedAt(pivot.position + pivot.up * .5f))
- {
- yield return StartCoroutine(DoCrawl(pivot.forward/2f, speed));
- }
- Debug.LogWarning("turn 1");
- yield return StartCoroutine(DoTurnToOpenCrawlSpace(pivot, turnSpeed, .5f, -1f));
- }
- else if (!groundAhead)
- {
- Debug.LogWarning("turn 2");
- yield return StartCoroutine(DoTurnToOpenCrawlSpace(pivot, turnSpeed, .5f, 1f));
- }
- }
- else
- {
- Debug.LogWarning("Stuck");
- yield return null;
- }
- }
- protected IEnumerator DoTurnToOpenCrawlSpace(Transform pivot, float speed, float dist, float turnDir = 1f, Tween.EaseType easeType = Tween.EaseType.Linear)
- {
- turning = true;
- float duration = 1f/speed;
- while (IsBlockedAt(pivot.position + pivot.up * .5f + pivot.forward * dist) || !IsGroundAt(pivot.position + pivot.up * .5f + pivot.forward * dist, -pivot.up))
- {
- bool blockedAhead = IsBlockedAt(pivot.position + pivot.up * .5f + pivot.forward * dist);
- bool groundAhead = IsGroundAt(pivot.position + pivot.up * .5f + pivot.forward * dist, -pivot.up);
- Debug.LogWarning("blockedAhead: " + blockedAhead + " groundAhead: " + groundAhead);
- var targetRotation = Quaternion.EulerAngles(turnAngle + turnDir * 90f, 0f, 0f);
- var startLocalRotation = pivot.localRotation;
- float startAngle = turnAngle;
- float endAngle = startAngle + 90f * turnDir;
- float timer = 0f;
- while (timer < duration)
- {
- /*
- timer += Time.deltaTime;
- if (timer > duration)
- timer = duration;
- float p = timer/duration;
- pivot.localRotation = Quaternion.Slerp(startLocalRotation, targetRotation, p);
- yield return null;
- */
- timer += Time.deltaTime;
- if (timer > duration)
- timer = duration;
- float p = timer/duration;
- pivot.localRotation = Quaternion.Euler(startAngle * (1f - p) + endAngle * p, 0f, 0f);
- //Vector3 localEulerAngles = pivot.localEulerAngles;
- // = startAngle * (1f - p) + endAngle * p;
- //pivot.localEulerAngles = localEulerAngles;
- yield return null;
- }
- //yield return new WaitForSeconds(.1f);
- turnAngle += turnDir * 90f;
- //var targetRotation = Quaternion.Slerp(pivot.localRotation, Quaternion.EulerAngles(pivot.localEulerAngles + Vector3.right * 90f * turnDir), 360f);
- //Tween.RotateTo(pivot.gameObject, pivot.localEulerAngles + Vector3.right * 90f * turnDir, time, easeType, true);
- //yield return new WaitForSeconds(time);
- }
- turning = false;
- yield return null;
- }
- protected IEnumerator DoCrawl(Vector3 move, float speed)
- {
- moving = true;
- float time = move.magnitude / speed;
- Tween.MoveTo(gameObject, transform.position + move, time, Tween.EaseType.Linear, true);
- yield return new WaitForSeconds(time);
- moving = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement