| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace WildAttack
- {
- /// <summary>
- /// 动物类
- /// </summary>
- public class Animal : MonoBehaviour
- {
- #region Members
- private Animator anim;
- /// <summary>
- /// 移动范围
- /// </summary>
- [SerializeField] private float targetRange;
- private Vector3 oriPos;
- private Vector3 randomPoint;
- [SerializeField] private float idlerestSpawnTime = 0;
- #endregion
- #region Lifecycle
- // Start is called before the first frame update
- void Start()
- {
- anim = GetComponent<Animator>();
- oriPos = transform.localPosition;
- idlerestSpawnTime = Random.Range(3f, 15f);
- GetNewRandomTarget();
- }
- // Update is called once per frame
- void Update()
- {
- // 判断死亡动画状态
- if (anim.GetCurrentAnimatorStateInfo(0).IsName("Dead") && anim.GetCurrentAnimatorStateInfo(0).normalizedTime > 0.9f)
- {
- Destroy(gameObject);
- }
- // 随机idle run
- float runWeight = Random.Range(0f, 1f);
- anim.SetFloat("run", runWeight);
- if (Vector3.Distance(transform.position, randomPoint) < 1f)
- {
- GetNewRandomTarget();
- }
- else
- {
- // 判断状态
- transform.LookAt(new Vector3(randomPoint.x, transform.position.y, randomPoint.z));
- if (anim.GetCurrentAnimatorStateInfo(0).IsName("Run") && idlerestSpawnTime > 0)
- {
- transform.Translate(Vector3.forward * 5 * Time.deltaTime);
- }
- }
- // idlerest动作3~15秒随机播放一次
- if (idlerestSpawnTime <= 0)
- {
- anim.SetTrigger("idlerest");
- idlerestSpawnTime = Random.Range(3f, 15f);
- }
- else
- {
- idlerestSpawnTime -= Time.deltaTime;
- }
- }
- #endregion
- #region Functions
- public void Dead()
- {
- anim.SetTrigger("dead");
- }
- /// <summary>
- /// 获取新目标点
- /// </summary>
- void GetNewRandomTarget()
- {
- float randomX = Random.Range(-targetRange, targetRange);
- float randomZ = Random.Range(-targetRange, targetRange);
- //会根据角当前位置的一定范围进行随机移动
- //Vector3 randomPint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);
- randomPoint = new Vector3(oriPos.x + randomX, transform.position.y, oriPos.z + randomZ);
- }
- #endregion
- }
- }
|