Advertisement
Guest User

Fish Wandering script

a guest
Jul 24th, 2022
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.64 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5.  
  6. public class FishWander : MonoBehaviour
  7. {
  8.     //Move vars
  9.     [Range(1.0f, 30.0f)]
  10.     public float deviateMaxAngle = 10.0f;
  11.     public float speed;
  12.     [Range(1.0f, 60.0f)]
  13.     public float seekDistance = 40.0f;
  14.     private Vector3 destination;
  15.     public float bumpDistance = 1f;
  16.     public float turnSpeed = 0.15f;
  17.     private bool needsMove = true;
  18.     Sequence movementSequence;
  19.     private bool hasCollided = false;
  20.  
  21.     //Flip Flap vars
  22.     public float swimSpeed;
  23.     public GameObject firstBone;
  24.     private bool flipFlapFinished = true;
  25.  
  26.     //Idle state vars
  27.     public Vector2 waitSecondsRandomRange;
  28.     private float waitSeconds;
  29.  
  30.     void Update()
  31.     {
  32.         if(needsMove){
  33.             MoveToDestination();
  34.         }
  35.  
  36.         if(flipFlapFinished)
  37.         {
  38.             FlipFlap();
  39.         }
  40.  
  41.         if(!hasCollided){
  42.             TryToCollide();
  43.         }
  44.     }
  45.  
  46.     void TryToCollide()
  47.     {
  48.             RaycastHit hit;
  49.             if(Physics.Raycast(transform.position, transform.forward, out hit, bumpDistance))
  50.             {
  51.                 Debug.DrawRay(transform.position, transform.forward * hit.distance, Color.white);
  52.                 hasCollided = true;
  53.                 movementSequence.Pause();
  54.                 MoveToRadicalDestination();
  55.             }
  56.     }
  57.  
  58.     void MoveToDestination()
  59.     {
  60.         needsMove = false;
  61.         GetDestination();
  62.         movementSequence = DOTween.Sequence();
  63.         movementSequence
  64.         .Append(transform.DOLookAt(destination, turnSpeed).SetEase(Ease.Linear))
  65.         .Append(transform.DOMove(destination, speed).SetEase(Ease.Linear))        
  66.         .AppendCallback(() =>
  67.             {
  68.                 StartCoroutine(StandIdle());
  69.             }
  70.         )
  71.         .SetAutoKill(true);
  72.     }
  73.  
  74.     void MoveToRadicalDestination()
  75.     {
  76.         needsMove = false;
  77.         GetRadicalDestination();
  78.         movementSequence = DOTween.Sequence();
  79.         movementSequence
  80.         .Append(transform.DOLookAt(destination, turnSpeed).SetEase(Ease.Linear))
  81.         .Append(transform.DOMove(destination, speed).SetEase(Ease.Linear))        
  82.         .AppendCallback(() =>
  83.             {
  84.                 hasCollided = false;
  85.                 StartCoroutine(StandIdle());
  86.             }
  87.         )
  88.         .SetAutoKill(true);
  89.     }
  90.  
  91.     void GetDestination()
  92.     {
  93.         Vector3 testPosition = (transform.position + (transform.forward * seekDistance)) +
  94.             new Vector3(
  95.                 Random.Range(deviateMaxAngle*-1, deviateMaxAngle),
  96.                 Random.Range(deviateMaxAngle*-1, deviateMaxAngle),
  97.                 Random.Range(deviateMaxAngle*-1, deviateMaxAngle)
  98.                 );
  99.         destination = testPosition;
  100.     }
  101.  
  102.     void GetRadicalDestination()
  103.     {
  104.         Debug.Log("Goes to Radical Destination");
  105.         Vector3 testPosition = (transform.position + (transform.forward * -1 * seekDistance));
  106.         destination = testPosition;
  107.     }
  108.  
  109.     void FlipFlap()
  110.     {
  111.         flipFlapFinished = false;
  112.         firstBone.transform.DOLocalRotate(new Vector3(0,0,12), swimSpeed, RotateMode.FastBeyond360).SetEase(Ease.InOutSine).SetRelative(true).SetLoops(2, LoopType.Yoyo)
  113.         .OnComplete(()=>{
  114.             flipFlapFinished = true;
  115.         }).SetAutoKill(true);
  116.     }
  117.  
  118.     IEnumerator StandIdle()
  119.     {
  120.         swimSpeed*=2f;
  121.         waitSeconds = Random.Range(waitSecondsRandomRange.x, waitSecondsRandomRange.y);
  122.         yield return new WaitForSeconds(waitSeconds);
  123.         swimSpeed*=0.5f;
  124.         needsMove = true;
  125.     }
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement