(没有找到新版动画的怪物素材)
过场cg:龙在角斗场飞行一周缓慢降落,走到场地中央
(测试场景)
喷火
飞行后爪攻击
龙的领域。。。
(人物技能。。。)
过场cg代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GuoChangMV : MonoBehaviour {
private Animation ani;
private Vector3 initPosition;
private Vector3 dir;
// Use this for initialization
void Start () {
ani = transform.GetComponent<Animation>();
initPosition = transform.position;
transform.Rotate(Vector3.up, -90);
transform.position = initPosition + new Vector3(0,14.6f,0);
}
// Update is called once per frame
void Update () {
dir = Vector3.zero - transform.position;
if (transform.position.y > 0.1)
{
transform.RotateAround(Vector3.zero, Vector3.up, 0.5f);
transform.position -= new Vector3(0, 0.02f, 0);
ani.Play("fly");
return;
}
else
{
transform.LookAt(Vector3.zero);
ani.Play("walk");
if (Vector3.Distance(transform.position , Vector3.zero)>0.5)
{
transform.position += dir.normalized * 0.08f;
}
else
{
transform.GetComponent<DragonAI>().enabled=true;
transform.GetComponent<GuoChangMV>().enabled = false;
}
}
}
}
龙的攻击方式
using System.Collections;
using UnityEngine;
public class DragonAI : MonoBehaviour {
//血量
private int hp = GameConsts.Dragonhp;
//龙状态
public const int STATE_LOAD = 0;
public const int STATE_FLY = 1;
public const int STATE_MOVE = 2;
public const int STATE_CHANGING = 3;
public const int STATE_BATTLING = 4;
//怪物当前状态
private int NowState;
private bool hasMoved=false;
private bool findPlayerToATK = false;
//龙初始化位置
private Vector3 initPosition;
//龙动画控制
private Animation ani;
public GameObject[] TeXiao;
//玩家们
private GameObject[] player;
//找最近、最远玩家
private GameObject maxPlayer;
private Vector3 dirToMax;
private GameObject minPlayer;
private Vector3 dirToMin;
private float maxDistance = 0, minDistance = 100;
//怪物思考时间
public const int AI_THINK_TIME = 2;
//龙发现玩家距离
public const int AI_FIND_PLAYER = 30;
//龙飞行状态切换陆地状态
public const int AI_FLY_TO_STAND = 30;
//龙陆地状态切换飞行状态
public const int AI_STAND_TO_FLY = 30;
public float ChangStateCD = 20;
//上一次思考的时间
private float LastThinkTime;
private float lastIdelTime;
private int idelAction = 2;
private float LinYuTime = 30;
private GameObject pen;
private GameObject pen1;
void Start()
{
transform.localEulerAngles = new Vector3(0.0f, 0.0f, 0.0f);
//初始化状态机
ani = transform.GetComponent<Animation>();
//记录初始化位置
initPosition = transform.position;
//找到所有玩家
player = GameObject.FindGameObjectsWithTag("Player");
NowState = STATE_LOAD;
pen = transform.GetChild(0).gameObject;
pen1 = transform.GetChild(1).gameObject;
}
void Update()
{
if (GameConsts.Dragonhp < 0)
{
ani.Play("death");
transform.GetComponent<DragonAI>().enabled = false;
}
FindPlayer();
if (findPlayerToATK == false)
{
Idel();
return;
}
if (Move()==true)
{
Think();
LinYu();
}
}
private void FindPlayer()
{
maxDistance = 0;
minDistance = 100;
foreach (GameObject i in player)
{
float Distance = Vector3.Distance(i.transform.position, transform.position);
if (Distance > maxDistance)
{
maxPlayer = i;
maxDistance = Distance;
}
if (Distance < minDistance)
{
minPlayer = i;
minDistance = Distance;
}
}
dirToMax = (maxPlayer.transform.position - transform.position).normalized;
dirToMin = (minPlayer.transform.position - transform.position).normalized;
if (minDistance < AI_FIND_PLAYER)
{ findPlayerToATK = true; }
}
private void ChangeState()
{
if (NowState == STATE_LOAD)
{
NowState = STATE_FLY;
Debug.Log("UP");
StartCoroutine("Fly_LOAD", Vector3.up);
return;
}
if(NowState == STATE_FLY)
{
NowState = STATE_LOAD;
Debug.Log("Down");
StartCoroutine("Fly_LOAD", Vector3.down);
return;
}
}
private void Think()
{
//先移动后攻击
if (Time.time - LastThinkTime > AI_THINK_TIME)
{
ani.Stop();
int Rnd;
//开始思考
LastThinkTime = Time.time;
//陆地状态战斗
if (NowState == STATE_LOAD)
{
Rnd = Random.Range(0, 4);
//根据随机数值为怪物赋予不同的状态行为
switch (Rnd)
{
case 0:
transform.LookAt(minPlayer.transform);
ani.Play("attack1");
break;
case 1:
transform.LookAt(minPlayer.transform);
ani.Play("attack2");
break;
case 2:
transform.Rotate(Vector3.up,180);
ani.Play("whip tail");
break;
case 3:
transform.LookAt(maxPlayer.transform);
ani.Play("breath fire");
GameObject FireSpit1 = Instantiate(TeXiao[2], pen.transform.position, pen.transform.rotation);
Destroy(FireSpit1, 5);
break;
}
}
//飞行状态战斗
if (NowState == STATE_FLY)
{ //获取0-3之间的随机数字
Rnd = Random.Range(0, 2);
//根据随机数值为怪物赋予不同的状态行为
switch (Rnd)
{
case 0:
transform.LookAt(new Vector3(minPlayer.transform.position.x, transform.position.y, minPlayer.transform.position.z));
ani.Play("fly attack");
break;
case 1:
transform.LookAt(new Vector3(maxPlayer.transform.position.x, transform.position.y, maxPlayer.transform.position.z));
ani.Play("fly breath fire");
GameObject FireSpit = Instantiate(TeXiao[2], pen1.transform.position, pen1.transform.rotation );
Destroy(FireSpit, 5);
break;
}
}
}
}
IEnumerator Fly_LOAD(Vector3 vector3)
{
NowState = STATE_CHANGING;
for (float cd = 2; cd >= 0; cd -= Time.deltaTime)
{
ani.Play("fly");
transform.position += vector3 * 0.03f;
yield return 0;
}
if (transform.position.y > 2)
{
NowState = STATE_FLY;
}
else
{
NowState = STATE_LOAD;
}
yield break;
}
public void Idel()
{
float idelTime = 10;
if (Time.time - lastIdelTime > idelTime)
{
lastIdelTime = Time.time;
if (idelAction == 1)
{ idelAction = 2; }
else
{ idelAction = 1; }
switch (idelAction)
{
case 1:
ani.Play("stand");
break;
case 2:
ani.Play("idle");
break;
}
}
}
public bool Move()
{
//飞行陆地切换时间控制
ChangStateCD -= Time.deltaTime;
if (ChangStateCD < 0)
{
ChangeState();
ChangStateCD = 20;
}
if (NowState == STATE_CHANGING)
{
return false;
}
if (NowState == STATE_FLY)
{
if (minDistance < 4)
return true;
transform.LookAt(new Vector3(minPlayer.transform.position.x, transform.position.y, minPlayer.transform.position.z));
ani.Play("fly");
transform.position += new Vector3(dirToMin.x, 0, dirToMin.z) * 0.12f;
}
if (minDistance < 8)
{
return true;
}
if (minDistance < 15)
{
if (NowState == STATE_LOAD)
{
transform.LookAt(minPlayer.transform);
ani.Play("walk");
transform.position += dirToMin * 0.09f;
}
else
{
transform.LookAt(new Vector3(minPlayer.transform.position.x,transform.position.y,minPlayer.transform.position.z));
ani.Play("fly");
transform.position += new Vector3(dirToMin.x,0,dirToMin.z) * 0.09f;
}
}
else if ( minDistance < 30)
{
if (NowState == STATE_LOAD)
{
transform.LookAt(minPlayer.transform);
ani.Play("run");
transform.position += dirToMin * 0.12f;
}
else
{
transform.LookAt(new Vector3(minPlayer.transform.position.x, transform.position.y, minPlayer.transform.position.z));
ani.Play("fly");
transform.position += new Vector3(dirToMin.x, 0, dirToMin.z) * 0.12f;
}
}
return false;
}
public void LinYu()
{
if (LinYuTime > 0)
{
LinYuTime -= Time.deltaTime;
}
else
{
GameObject JuanLinYu = Instantiate(TeXiao[0], new Vector3(transform.position.x, 0, transform.position.z), transform.rotation);
GameObject FireLinYu = Instantiate(TeXiao[1], new Vector3(transform.position.x, 0, transform.position.z), transform.rotation);
JuanLinYu.transform.rotation = Quaternion.Euler(0,0,0);
FireLinYu.transform.rotation = Quaternion.Euler(0, 0, 0);
Destroy(JuanLinYu, 15);
Destroy(FireLinYu, 15);
LinYuTime = 35;
}
}
}