Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using DG.Tweening;
- public class FishWander : MonoBehaviour
- {
- //Move vars
- [Range(1.0f, 30.0f)]
- public float deviateMaxAngle = 10.0f;
- public float speed;
- [Range(1.0f, 60.0f)]
- public float seekDistance = 40.0f;
- private Vector3 destination;
- public float bumpDistance = 1f;
- public float turnSpeed = 0.15f;
- private bool needsMove = true;
- Sequence movementSequence;
- private bool hasCollided = false;
- //Flip Flap vars
- public float swimSpeed;
- public GameObject firstBone;
- private bool flipFlapFinished = true;
- //Idle state vars
- public Vector2 waitSecondsRandomRange;
- private float waitSeconds;
- void Update()
- {
- if(needsMove){
- MoveToDestination();
- }
- if(flipFlapFinished)
- {
- FlipFlap();
- }
- if(!hasCollided){
- TryToCollide();
- }
- }
- void TryToCollide()
- {
- RaycastHit hit;
- if(Physics.Raycast(transform.position, transform.forward, out hit, bumpDistance))
- {
- Debug.DrawRay(transform.position, transform.forward * hit.distance, Color.white);
- hasCollided = true;
- movementSequence.Pause();
- MoveToRadicalDestination();
- }
- }
- void MoveToDestination()
- {
- needsMove = false;
- GetDestination();
- movementSequence = DOTween.Sequence();
- movementSequence
- .Append(transform.DOLookAt(destination, turnSpeed).SetEase(Ease.Linear))
- .Append(transform.DOMove(destination, speed).SetEase(Ease.Linear))
- .AppendCallback(() =>
- {
- StartCoroutine(StandIdle());
- }
- )
- .SetAutoKill(true);
- }
- void MoveToRadicalDestination()
- {
- needsMove = false;
- GetRadicalDestination();
- movementSequence = DOTween.Sequence();
- movementSequence
- .Append(transform.DOLookAt(destination, turnSpeed).SetEase(Ease.Linear))
- .Append(transform.DOMove(destination, speed).SetEase(Ease.Linear))
- .AppendCallback(() =>
- {
- hasCollided = false;
- StartCoroutine(StandIdle());
- }
- )
- .SetAutoKill(true);
- }
- void GetDestination()
- {
- Vector3 testPosition = (transform.position + (transform.forward * seekDistance)) +
- new Vector3(
- Random.Range(deviateMaxAngle*-1, deviateMaxAngle),
- Random.Range(deviateMaxAngle*-1, deviateMaxAngle),
- Random.Range(deviateMaxAngle*-1, deviateMaxAngle)
- );
- destination = testPosition;
- }
- void GetRadicalDestination()
- {
- Debug.Log("Goes to Radical Destination");
- Vector3 testPosition = (transform.position + (transform.forward * -1 * seekDistance));
- destination = testPosition;
- }
- void FlipFlap()
- {
- flipFlapFinished = false;
- firstBone.transform.DOLocalRotate(new Vector3(0,0,12), swimSpeed, RotateMode.FastBeyond360).SetEase(Ease.InOutSine).SetRelative(true).SetLoops(2, LoopType.Yoyo)
- .OnComplete(()=>{
- flipFlapFinished = true;
- }).SetAutoKill(true);
- }
- IEnumerator StandIdle()
- {
- swimSpeed*=2f;
- waitSeconds = Random.Range(waitSecondsRandomRange.x, waitSecondsRandomRange.y);
- yield return new WaitForSeconds(waitSeconds);
- swimSpeed*=0.5f;
- needsMove = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement