Files
2026-03-04 10:03:45 +08:00

53 lines
1.2 KiB
C#

using UnityEngine;
public class FlyBird : MonoBehaviour
{
public float normalSpeed = 1f;
public float minHeightFly = 5f;
public float maxHeightFly = 15f;
public float range = 20f;
private Vector3 startPosition;
private Vector3 dstPoint;
private float currSpeed = 1f;
private void Start()
{
startPosition = base.transform.position;
RandomDstPoint();
currSpeed = normalSpeed;
}
private void Update()
{
base.transform.Translate(Vector3.forward * Time.deltaTime * currSpeed);
Vector3 vector = base.transform.position - dstPoint;
if (!(vector == Vector3.zero))
{
Quaternion b = Quaternion.LookRotation(vector * Time.deltaTime);
base.transform.rotation = Quaternion.Slerp(base.transform.rotation, b, Time.deltaTime * (1f + currSpeed));
if (Vector3.Distance(base.transform.position, dstPoint) < 1f)
{
RandomDstPoint();
}
}
}
private void RandomDstPoint()
{
dstPoint = startPosition + Random.insideUnitSphere * range;
dstPoint.y = startPosition.y + Random.Range(minHeightFly, maxHeightFly);
currSpeed = normalSpeed + Random.Range(0f, 3f);
}
private void OnDrawGizmosSelected()
{
Gizmos.DrawWireSphere(base.transform.position, range);
}
}