260 lines
7.8 KiB
C#
260 lines
7.8 KiB
C#
// ***********************************************************************
|
|
// Assembly : Unity
|
|
// Author : Kimch
|
|
// Created : 2018-2-29
|
|
// Description : 战骑
|
|
// Last Modified By :
|
|
// Last Modified On :
|
|
// ***********************************************************************
|
|
// <copyright file= "Pet_eagle" company=""></copyright>
|
|
// <summary></summary>
|
|
// ***********************************************************************
|
|
using UnityEngine;
|
|
|
|
namespace G
|
|
{
|
|
public class Pet_eagle : MonoBehaviour
|
|
{
|
|
#region Field
|
|
|
|
private Transform _cha;
|
|
|
|
private Vector3 _rndPos;
|
|
|
|
private float _skillDelay = 2f;
|
|
|
|
private bool _skillOn;
|
|
|
|
private Vector3 _direction;
|
|
|
|
private Transform _findItem;
|
|
|
|
private int _itemBehaviour;
|
|
|
|
private Quaternion _rotate;
|
|
|
|
private Vector3 _edgePos;
|
|
|
|
private bool _aniSpeedChange;
|
|
|
|
private float _flySpeed = 0.5f;
|
|
|
|
private float _finishDelay = -3f;
|
|
|
|
private float _getItemDelay;
|
|
|
|
private Animation _animation;
|
|
|
|
#endregion
|
|
|
|
#region Method
|
|
|
|
public void LoadDir()
|
|
{
|
|
_rndPos = _cha.position + _edgePos;
|
|
_direction = _rndPos - transform.position;
|
|
_direction.y = 0f;
|
|
if (_direction != Vector3.zero)
|
|
{
|
|
_rotate = Quaternion.LookRotation(_direction);
|
|
}
|
|
}
|
|
|
|
public void SetRndPosition()
|
|
{
|
|
_edgePos = Random.insideUnitSphere * 0.1f;
|
|
_edgePos.y = 0.3f;
|
|
var currentMotion = Random.Range(0, 10);
|
|
if (currentMotion < 7 || _skillOn)
|
|
{
|
|
_animation.CrossFade("fly_flap");
|
|
}
|
|
else
|
|
{
|
|
_animation.CrossFade("fly_glide");
|
|
}
|
|
}
|
|
|
|
public void SkillOn()
|
|
{
|
|
_skillOn = true;
|
|
_aniSpeedChange = true;
|
|
}
|
|
|
|
public void SkillOff()
|
|
{
|
|
_skillOn = false;
|
|
_aniSpeedChange = true;
|
|
}
|
|
|
|
public void GetItem(Transform findItem)
|
|
{
|
|
if (_itemBehaviour == 0 && !_skillOn)
|
|
{
|
|
_finishDelay = 5.5f;
|
|
_itemBehaviour = 1;
|
|
_findItem = findItem;
|
|
|
|
_direction = _findItem.position - transform.position;
|
|
_direction.y = 0f;
|
|
CancelInvoke(nameof(LoadDir));
|
|
CancelInvoke(nameof(SetRndPosition));
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Unity
|
|
|
|
private void Awake()
|
|
{
|
|
_animation = this.GetComponent<Animation>();
|
|
_cha = GameObject.FindWithTag("Player").transform;
|
|
|
|
}
|
|
|
|
public void OnEnable()
|
|
{
|
|
InvokeRepeating(nameof(LoadDir), 0.1f, 0.1f);
|
|
InvokeRepeating(nameof(SetRndPosition), 0.1f, 2f);
|
|
|
|
_finishDelay = 0f;
|
|
float x = 2 * Random.Range(0, 2) - 1;
|
|
float z = 2 * Random.Range(0, 2) - 1;
|
|
_edgePos = new Vector3(x * 0.6f, 0.3f, z * 0.5f);
|
|
this.transform.position = _cha.position + _edgePos;
|
|
_flySpeed = 0.5f;
|
|
_animation.CrossFade("fly_flap");
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
_animation["fly_glide"].layer = 1;
|
|
_animation["fly_glide"].speed = 0.4f;
|
|
|
|
_animation["fly_flap"].layer = 1;
|
|
_animation["fly_flap"].speed = 0.4f;
|
|
_finishDelay = -3f;
|
|
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (_skillOn)
|
|
{
|
|
if (_skillDelay > 0f)
|
|
{
|
|
transform.position = Vector3.Lerp(transform.position, _cha.position + Vector3.up * 0.3f, Time.deltaTime * 6f);
|
|
_skillDelay -= Time.deltaTime;
|
|
var tochaV = _cha.position - transform.position;
|
|
tochaV.y = 0f;
|
|
if (tochaV != Vector3.zero)
|
|
{
|
|
transform.rotation = Quaternion.LookRotation(tochaV);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
transform.position = _cha.position + Vector3.up * 0.3f;
|
|
transform.rotation = Quaternion.Lerp(transform.rotation, _cha.rotation, Time.deltaTime * 3f);
|
|
if (_aniSpeedChange)
|
|
{
|
|
_animation["fly_flap"].speed = 0.7f;
|
|
_aniSpeedChange = false;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (_itemBehaviour == 1)
|
|
{
|
|
if (_findItem.gameObject.activeSelf)
|
|
{
|
|
_animation.CrossFade("fly_flap");
|
|
_getItemDelay += Time.deltaTime;
|
|
if (_getItemDelay > 2.5f)
|
|
{
|
|
_getItemDelay = 0f;
|
|
_itemBehaviour = 2;
|
|
}
|
|
else if (_getItemDelay > 2.2f)
|
|
{
|
|
_findItem.position = Vector3.MoveTowards(_findItem.position, transform.position, Time.deltaTime);
|
|
}
|
|
transform.position = Vector3.Lerp(transform.position, _findItem.position, Time.deltaTime * 2f);
|
|
if (_direction != Vector3.zero)
|
|
{
|
|
transform.rotation = Quaternion.LookRotation(_direction);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_itemBehaviour = 0;
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (_itemBehaviour == 2)
|
|
{
|
|
if (_findItem.gameObject.activeSelf)
|
|
{
|
|
_getItemDelay += Time.deltaTime;
|
|
if (_getItemDelay > 2f)
|
|
{
|
|
_itemBehaviour = 0;
|
|
_getItemDelay = 0f;
|
|
return;
|
|
}
|
|
|
|
if (_getItemDelay > 1f)
|
|
{
|
|
_findItem.position = Vector3.MoveTowards(_findItem.position, _cha.position, Time.deltaTime * 1.2f);
|
|
transform.position += transform.forward * Time.deltaTime * 0.8f;
|
|
return;
|
|
}
|
|
|
|
_direction = _cha.position - transform.position;
|
|
_direction.y = 0f;
|
|
if (_direction != Vector3.zero)
|
|
{
|
|
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(_direction), Time.deltaTime * 3f);
|
|
}
|
|
transform.position = Vector3.Lerp(transform.position, _cha.position + Vector3.up * 0.3f, Time.deltaTime * 2f);
|
|
_findItem.position = transform.position - Vector3.up * 0.1f;
|
|
}
|
|
else
|
|
{
|
|
_itemBehaviour = 0;
|
|
}
|
|
return;
|
|
}
|
|
|
|
_finishDelay += Time.deltaTime;
|
|
if (_finishDelay > 8f)
|
|
{
|
|
_flySpeed = 1f;
|
|
CancelInvoke(nameof(LoadDir));
|
|
CancelInvoke(nameof(SetRndPosition));
|
|
|
|
if (_finishDelay > 9.5f)
|
|
{
|
|
this.gameObject.SetActive(false);
|
|
this.transform.position = Vector3.one * 8f;
|
|
}
|
|
}
|
|
|
|
if (_aniSpeedChange)
|
|
{
|
|
_animation["fly_flap"].speed = 0.4f;
|
|
_aniSpeedChange = false;
|
|
}
|
|
|
|
_skillDelay = 2f;
|
|
this.transform.rotation = Quaternion.Lerp(transform.rotation, _rotate, Time.deltaTime * 1.6f);
|
|
this.transform.position += transform.forward * Time.deltaTime * _flySpeed;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|