unity3d 代码求个游戏的代码

文章 - 0&评论 - 0&trackbacks - 0
* 怪物基类,实现了怪物一些属性,死亡
9 using UnityE
10 using System.C
13 public class Enemy : MonoBehaviour {
public GameObject IceE
public GameObject FireE
public BossType bossoneD = BossType.BossOne1;//BOSS状态 用来设置BOSS掉落
public int[] BoxDropArray = { 2, 2, 0, 0, 0, 0, 15, 21 };
public GameObject ImpactE
public string _name = "a";
//怪物名字
public float _maxHealth = 1;
//最大生命值
public float _curHealth = 1;
//当前生命值
public Transform MyT
public EnemyType m_EnemyState = EnemyType.N
public bool _isAlive = true; //怪物是否活着
public float _moveSpeed=3.5f;
//怪物移动速度
public int _rotationS
//怪物旋转速度
public Transform _//目标
public int maxAttackD
//攻击距离
public int maxCordonD
//警戒距离
public float DeadDestoryT
public CharacterC
public Animation MyA
public GameObject EnemyD//掉落的物品
NavMeshA//用于存储Robot身上附带的NavMeshAgent组件
public int ArmorPiercingCount = 0;//穿甲弹记录
private float ArmorPiercingTimer = 0.0f;//穿甲弹归零记录。
public SenceManager m_SenceM
public bool m_CanBeAttacked = true;//可以被攻击
private bool DropGoldbl = true;
public GameObject m_GoldE//爆金币预设
public GameObject HPB//血条
public GameObject DeathE
public bool m_ShowHpBar = false;
private bool PlayerD
private AudioManager m_audioM
void Awake()
m_audioManager = GameObject.FindGameObjectWithTag ("Audio").GetComponent&AudioManager& ();
public void SetShowHpBar(bool bl)
m_ShowHpBar =
/// &summary&
/// 设置最大血量
/// &/summary&
public void SetMaxHp(float health)
/// &summary&
/// 设置特效
/// &/summary&
public void SetEffect()
if ((m_EnemyOnSlowDown || m_EnemyOnFrozen) && IceEffect)
IceEffect.SetActive(true);
IceEffect.SetActive(false);
if (m_EnemyOnFire && FireEffect)
FireEffect.SetActive(true);
FireEffect.SetActive(false);
/// &summary&
/// 添加敌人血条
/// &/summary&
public void AddEnemyBar()
GameObject hpbar = Instantiate(HPBar, Vector3.zero, Quaternion.identity) as GameO
hpbar.SendMessage("SetTarget", this.gameObject);
hpbar.transform.localScale = new Vector3(1.6f, 0.5f, 0);
/// &summary&
/// &/summary&
public void ArmorPiercingCheck()
if (ArmorPiercingCount&0)
ArmorPiercingTimer += Time.deltaT
if (ArmorPiercingTimer&=1.5f)
ArmorPiercingTimer = 0;
ArmorPiercingCount = 0;
// Use this for initialization
void Start () {
AllStartInit();
private GameObject DamageE
/// &summary&
/// 设置目标
/// &/summary&
public void SetTarget(GameObject go)
_target = go.
public GameO//玩家
public PlayerControl m_PlayerC
public void AllStartInit()
player= GameObject.FindGameObjectWithTag("Player");
m_PlayerControl = player.GetComponent&PlayerControl&();
PlayerDead = m_PlayerControl.isA
MyTransform = this.
m_SenceManager = GameObject.Find("SenceManager").GetComponent&SenceManager&();
cc = this.GetComponent&CharacterController&();
MyAnimation = this.
nma = GetComponent&NavMeshAgent&();
private ContrlAi _curP//
private Vector3 InitP//初始坐标
public int i = 0;
public float m_EnemyD
// Update is called once per frame
void Update()
if (!m_PlayerControl.isAlive)
ChildrenUpdataALL();
if (_isAlive)
SetEffect();
if (!_target)
SetTarget(player);
FaceToTarget(_target.gameObject);
Debug.DrawLine(_target.position, MyTransform.position, Color.red);
float DistanceWithPlayer = Vector3.Distance(player.transform.position, transform.position);
if (m_EnemyOnChaos)
OnChaos();
if (m_EnemyOnSlowDown)//减速
m_SlowDownTimer += Time.deltaT
if (m_SlowDownTimer&=m_SlowDownCD)
m_EnemyOnSlowDown = false;
m_SlowDownTimer = 0.0f;
SlowDownLV = 0;
if (m_EnemyOnFrozen)//如果在冰冻状态
nma.speed = 0;
FrozenTimer += Time.deltaT//冰冻时间累加器
if (FrozenTimer&=FrozenTimeCD)
m_EnemyOnFrozen = false;
FrozenTimer = 0;
nma.speed = _moveSpeed*(1-0.2f*SlowDownLV);
nma.SetDestination(_target.position);//设置目的地
if (DistanceWithPlayer&=maxCordonDistance)
SetTarget(player);
if (DistanceWithPlayer&= maxAttackDistance)
if (damagecounti &= 0)
player.SendMessage("OnDamage", m_EnemyDamage);
_curHealth -= 1000;
//EnemyOnDamage(1000);
//设置最大血量
public void SetMaxHealth(float hp)
_maxHealth =
/// &summary&
/// 面向目标
/// &/summary&
/// &param name="target"&&/param&
public void FaceToTarget(GameObject target)
//朝向玩家 y轴不变
MyTransform.rotation = Quaternion.Slerp(MyTransform.rotation, Quaternion.LookRotation(new Vector3(target.transform.position.x, 0, target.transform.position.z) - new Vector3(MyTransform.position.x, 0, MyTransform.position.z)), _rotationSpeed * Time.deltaTime);
public float damageTime = 0.2f;
/// &summary&
/// 判断一定时间内是否受到伤害
/// &/summary&
/// &returns&&/returns&
public bool isDamage()
bool bl = false;
if (damageTime &= 0)
bl = true;
bl = false;
public bool m_EnemyOnChaos = false;//混乱状态
private float ChaosTimeCD = 5;//混乱持续时间
private float ChaosTimer = 0;//混乱累加器
public bool m_EnemyOnFrozen = false;//冰冻状态
public float FrozenTimeCD = 5;//冰冻持续时间
public float FrozenTimer = 0;//冰冻累加器
public bool m_EnemyOnSlowDown = false;//减速
public bool m_EnemyOnFire = false;//燃烧状态
private float FireTimeMiniCD = 1;
private int FireCount = 0;
private float FireTimer = 0;
/// &summary&
/// 判断计算燃烧 放在update里面 内嵌了减速判断
/// &/summary&
public void Burning()
if (m_EnemyOnFire)//判断是否燃烧 如果燃烧
FireTimer += Time.deltaT
//燃烧一次 1秒掉一次血
if (FireTimer&=FireTimeMiniCD)
FireCount++;
FireTimer = 0;
EnemyOnDamage(5);
//如果燃烧了3次 则不在燃烧
if (FireCount&=3)
FireCount = 0;//燃烧计数重置
m_EnemyOnFire = false;
if (m_EnemyOnSlowDown)//减速
m_SlowDownTimer += Time.deltaT
if (m_SlowDownTimer &= m_SlowDownCD)
m_EnemyOnSlowDown = false;
m_SlowDownTimer = 0.0f;
SlowDownLV = 0;
/// &summary&
/// 判断混乱状态 放到update里面
/// &/summary&
public void OnChaos()
//如果混乱中
if (m_EnemyOnChaos)
ChaosTimer += Time.deltaT//开始计时
SetChaosWalkPoint();//每2秒更换一次跑的地点
if (ChaosTimer&= ChaosTimeCD)//混乱时间到
ChaosTimer = 0;//时间制空
ChaosWalkTimer = 0.0f;
m_EnemyOnChaos = false;//将混乱状态置F
private float ChaosWalkCD = 1.0f;
private float ChaosWalkTimer = 1.0f;
/// &summary&
/// 每2秒设置一次混乱状态奔跑点
/// &/summary&
void SetChaosWalkPoint()
ChaosWalkTimer += Time.deltaT
if (ChaosWalkTimer&=ChaosWalkCD)
nma.SetDestination(new Vector3(this.transform.position.x + Random.Range(-3.0f, 3.0f), this.transform.position.y,this.transform.position.z + Random.Range(-3.0f, 3.0f)));//设置目的地
ChaosWalkTimer = 0;
/// &summary&
/// 收到电球攻击混乱
/// &/summary&
void EnemyOnElectBall()
m_EnemyOnChaos = true;
public int SlowDownLV = 0;//减速等级
public float m_SlowDownTimer = 0.0f;
public float m_SlowDownCD = 5.0f;
/// &summary&
/// 敌人受到减速攻击 Speed = _mySpeed*(1-0.2*SlowDownLV)
/// &/summary&
void EnemyOnSlowDown()
m_audioManager.Enemy_Hit();
//播放怪物受击音效
m_SlowDownTimer = 0.0f;//重置减速
m_EnemyOnSlowDown = true;//减速
SlowDownLV++;
if (SlowDownLV &=5)
EnemyOnIceBall();
SlowDownLV = 0;
m_EnemyOnSlowDown = false;//关闭减速
m_SlowDownTimer = 0.0f;//重置减速
/// &summary&
/// 受到火球攻击燃烧
/// &/summary&
void EnemyOnFireBall()
FireCount = 0;
m_EnemyOnFire = true;
/// &summary&
/// 受到冰球攻击冰冻
/// &/summary&
void EnemyOnIceBall()
m_EnemyOnFrozen = true;
public GameObject HP
/// &summary&
/// 受到伤害
/// &/summary&
/// &param name="damage"&&/param&
public void EnemyOnDamage(int damage)
if (m_CanBeAttacked && _isAlive)
GameO//= Instantiate(ImpactEffect, MyTransform.position, MyTransform.rotation) as GameO
//求通过暴击率更改伤害值
PlayerControl m_PlayerControl = player.GetComponent&PlayerControl&();
GameObject hplabel = Instantiate(HPlable, Vector3.zero, Quaternion.identity) as GameO
switch (m_EnemyState)
case EnemyType.EnemyBomb:
m_audioManager.Enemy_Hit();
//播放怪物受击音效
goo = Instantiate(ImpactEffect, MyTransform.position, MyTransform.rotation) as GameO
case EnemyType.EnemyBomb2:
m_audioManager.Enemy_Hit();
//播放怪物受击音效
goo = Instantiate(ImpactEffect, MyTransform.position, MyTransform.rotation) as GameO
case EnemyType.EnemyShot:
m_audioManager.Enemy_Hit();
//播放怪物受击音效
goo = Instantiate(ImpactEffect, MyTransform.position, MyTransform.rotation) as GameO
case EnemyType.EnemyFight:
m_audioManager.Enemy_Hit();
//播放怪物受击音效
goo = Instantiate(ImpactEffect, MyTransform.position, MyTransform.rotation) as GameO
case EnemyType.EnemyShot2:
m_audioManager.Enemy_Hit();
//播放怪物受击音效
goo = Instantiate(ImpactEffect, MyTransform.position, MyTransform.rotation) as GameO
case EnemyType.EnemyFight2:
m_audioManager.Enemy_Hit();
//播放怪物受击音效
goo = Instantiate(ImpactEffect, MyTransform.position, MyTransform.rotation) as GameO
case EnemyType.EnemyAOE:
case EnemyType.BossOne:
//m_audioManager.Enemy_Hit();
//播放怪物受击音效
case EnemyType.BossTwo:
//m_audioManager.Enemy_Hit();
//播放怪物受击音效
case EnemyType.BossThree:
//sm_audioManager.Enemy_Hit();
//播放怪物受击音效
case EnemyType.BossFour:
case EnemyType.BossFive:
case EnemyType.Null:
case EnemyType.Enemylair:
float CritRate = m_PlayerControl.CritR
float rand = Random.Range(0.01f,10.0f);
if (rand&=CritRate)//暴击
Vector3 vec = new Vector3(3.0f,3.0f,3.0f);
hplabel.SendMessage("m_SetScale", vec);
hplabel.GetComponent&UI_DamageLabel&().SetBaoji();
hplabel.GetComponent&UILabel&().color = Color.
damage *= 2;
hplabel.GetComponent&UILabel&().color = Color.
hplabel.SendMessage("SetTarget", this.gameObject);
hplabel.SendMessage("SetValue", damage);
if (_curHealth & 0)// && isDamage())
_curHealth -=
damageTime = 0.2f;
public int CurrentDamage = 0;
/// &summary&
/// 生命限制事件
/// &/summary&
public void EnemyVital()
#region 生命溢出限制
if (_curHealth & _maxHealth)
_curHealth = _maxH
if (_curHealth &= 0)
_curHealth = 0;
_isAlive = false;
nma.speed = 0;
#endregion
public float FOWtime = 3;
/// &summary&
/// 子类调用的通用事件 死亡 生命限制 燃烧
/// &/summary&
public void ChildrenUpdataALL()
FOWtime -= Time.deltaT
if (FOWtime&= 0)
CurrentDamage = 0;
FOWtime = 3;
damageTime -= Time.deltaT
DeathEvent();//死亡事件
EnemyVital();//生命溢出事件
Burning();//燃烧
ArmorPiercingCheck();//穿甲弹检测
public float DropR//= 2.0f;//1~10
public int DropCount = 0;
public GameObject m_BombE
int m_BombEffCount = 0;
int m_BombEffCount2=0;
int m_fight1sound=0;
int m_shot1sound=0;
int m_shot2sound=0;
//自爆怪1死亡特效
void BombOneEffect()
if (m_BombEffCount == 0)
m_audioManager.Bomb1_Die();
Instantiate(m_BombEffect, this.transform.position, this.transform.rotation);
m_BombEffCount++;
//自爆怪2死亡特效
void BombTwoEffect()
if (m_BombEffCount2 == 0)
m_audioManager.Bomb2_Die();
Instantiate(m_BombEffect, this.transform.position, this.transform.rotation);
m_BombEffCount2++;
//肉搏怪1死亡声音
void FightOneDieSound()
if (m_fight1sound==0)
m_audioManager.Fight1_Die();
m_fight1sound++;
//射击怪1死亡声音
void ShotOneDieSound()
if (m_shot1sound==0)
m_audioManager.Shot1_Die();
m_shot1sound++;
//射击怪2死亡声音
void ShotTwoDieSound()
if (m_shot2sound==0)
m_audioManager.Shot2_Die();
m_shot2sound++;
IEnumerator m_InstantiatBombEff()
yield return new WaitForSeconds(5.2f);
Instantiate(m_BombEffect, this.transform.position, this.transform.rotation);
/// &summary&
/// 死亡事件 渐渐下落 然后销毁
/// &/summary&
public void DeathEvent()
if (!_isAlive)
//碰撞器false
cc.enabled = false;
if (m_EnemyState == EnemyType.EnemyBomb)
BombOneEffect();
else if(m_EnemyState == EnemyType.EnemyBomb2)
BombTwoEffect();
else if (m_EnemyState==EnemyType.EnemyFight)
FightOneDieSound();
else if (m_EnemyState==EnemyType.EnemyFight2)
FightOneDieSound();
else if (m_EnemyState==EnemyType.EnemyShot)
ShotOneDieSound();
else if (m_EnemyState==EnemyType.EnemyShot2)
ShotTwoDieSound();
//PlayDeathActionOnce();
this.transform.Translate(0,-0.002f,0);
DeadDestoryTime -= Time.deltaT
int RandKey = Random.Range(1,10);
if (DropCount&=0)//死亡后设置一次掉落
if (RandKey &= DropRange)//判断血瓶掉落几率
if (EnemyDrop)//掉落血瓶
Instantiate(EnemyDrop, this.transform.position, this.transform.rotation);
if (m_GoldExp && DropGoldbl)
DropGold();
//Instantiate(m_GoldExp,this.transform.position,this.transform.rotation);
DropCount++;
if (DeadDestoryTime &= 0)
GameManager.GetInstance().m_KillCount++;
SenceSetCount();
Destroy(this.gameObject);
private float m_GoldDropRang = 0;//0.1 ~ 1
private int m_GoldDropCountMin = 0;//最小值
private int m_GoldDropCountMax = 0;//最大值
/// &summary&
/// 掉落金币
/// &/summary&
public void DropGold()
switch (m_EnemyState)
case EnemyType.EnemyBomb:
m_GoldDropRang = 0.33f;
m_GoldDropCountMin = 7;
m_GoldDropCountMax = 9;
case EnemyType.EnemyBomb2:
m_GoldDropRang =
m_GoldDropCountMin = 7;
m_GoldDropCountMax = 9;
case EnemyType.EnemyFight:
m_GoldDropRang = 0.33f;
m_GoldDropCountMin = 7;
m_GoldDropCountMax = 9;
case EnemyType.EnemyFight2:
m_GoldDropRang =
m_GoldDropCountMin = 7;
m_GoldDropCountMax = 9;
case EnemyType.EnemyShot:
m_GoldDropRang =
m_GoldDropCountMin = 7;
m_GoldDropCountMax = 9;
case EnemyType.EnemyShot2:
m_GoldDropRang = 0.33f;
m_GoldDropCountMin = 7;
m_GoldDropCountMax = 9;
case EnemyType.EnemyAOE:
m_GoldDropRang =
m_GoldDropCountMin = 7;
m_GoldDropCountMax = 9;
case EnemyType.BossOne:
m_GoldDropRang = 1f;
m_GoldDropCountMin = 15;
m_GoldDropCountMax = 26;
case EnemyType.BossTwo:
m_GoldDropRang = 1f;
m_GoldDropCountMin = 15;
m_GoldDropCountMax = 26;
case EnemyType.BossThree:
m_GoldDropRang = 1f;
m_GoldDropCountMin = 15;
m_GoldDropCountMax = 26;
case EnemyType.BossFour:
m_GoldDropRang = 1f;
m_GoldDropCountMin = 15;
m_GoldDropCountMax = 26;
case EnemyType.BossFive:
m_GoldDropRang = 1f;
m_GoldDropCountMin = 15;
m_GoldDropCountMax = 26;
case EnemyType.Null:
case EnemyType.Enemylair:
m_GoldDropRang = 1f;
m_GoldDropCountMin = 3;
m_GoldDropCountMax = 6;
float rdm = Random.Range(0.1f,1.0f);
if (rdm &= m_GoldDropRang)//如果在爆率中则爆金币
GameObject go = Instantiate(m_GoldExp, this.transform.position, this.transform.rotation) as GameO
go.GetComponent&GoldExp&().m_GoldCount = Random.Range(m_GoldDropCountMin,m_GoldDropCountMax);
/// &summary&
/// 设置金币掉落
/// &/summary&
public void SetGoldFasle()
DropGoldbl = false;
/// &summary&
/// 添加某种类型怪物击杀数。
/// &/summary&
public void SenceSetCount()
switch (m_EnemyState)
case EnemyType.EnemyBomb:
m_SenceManager.SetCount(GameEndCountType.EnemyBomb);
case EnemyType.EnemyShot:
m_SenceManager.SetCount(GameEndCountType.EnemyShot);
case EnemyType.EnemyFight:
m_SenceManager.SetCount(GameEndCountType.EnemyFight);
case EnemyType.EnemyAOE:
m_SenceManager.SetCount(GameEndCountType.EnemyAOE);
case EnemyType.BossOne:
m_SenceManager.SetCount(GameEndCountType.BossOne);
case EnemyType.BossTwo:
m_SenceManager.SetCount(GameEndCountType.BossTwo);
case EnemyType.BossThree:
m_SenceManager.SetCount(GameEndCountType.BossThree);
case EnemyType.BossFour:
m_SenceManager.SetCount(GameEndCountType.BossFour);
case EnemyType.BossFive:
m_SenceManager.SetCount(GameEndCountType.BossFive);
case EnemyType.Null:
case EnemyType.Enemylair:
int damagecounti = 0;
private void PlayDeathActionOnce()
if (i &= 0)
MyAnimation.Play("death");
if (MyAnimation["death"].time & MyAnimation["death"].length - 0.1f)
MyAnimation.Stop();
BOSS属性类
1 using UnityE
2 using System.C
5 public enum BossType
BossThree1,
BossThree2,
BossThree3
19 public class EnemyBoss :Enemy
private BossA
private AudioManager m_
void Start()
m_audiomanager = GameObject.FindGameObjectWithTag ("Audio").GetComponent&AudioManager& ();
AddEnemyBar();
AllStartInit();
bossai = this.GetComponent&BossAi&();
// Update is called once per frame
void Update()
if (!m_PlayerControl.isAlive)
if (!_isAlive)
MyTransform.position += new Vector3(0, 0.5f, 0);
ChildrenUpdataALL();
Burning();
/// &summary&
/// 判断一定时间内是否受到伤害
/// &/summary&
/// &returns&&/returns&
public bool isDamage()
bool bl = false;
if (damageTime &= 0)
bl = true;
bl = false;
public GameObject HitP
public GameObject HitE
public void SetBoxArray(int[] a)
BoxDropArray =
/// &summary&
/// 受到伤害
/// &/summary&
/// &param name="damage"&&/param&
public new void EnemyOnDamage(int damage)
//求通过暴击率更改伤害值
PlayerControl m_PlayerControl = player.GetComponent&PlayerControl&();
float CritRate = m_PlayerControl.CritR
float rand = Random.Range(0.01f, 10.0f);
if (_curHealth & 0)// && isDamage())
if (!bossai.Rushing)
GameObject hplabel = Instantiate(HPlable, Vector3.zero, Quaternion.identity) as GameO
m_audiomanager.Enemy_Hit();
//播放受击声音
Instantiate(HitEffect, HitPoint.transform.position, HitPoint.transform.rotation);
if (rand &= CritRate)//暴击
Vector3 vec = new Vector3(3.0f, 3.0f, 3.0f);
hplabel.SendMessage("m_SetScale", vec);
hplabel.GetComponent&UILabel&().color = Color.
damage *= 2;
hplabel.GetComponent&UILabel&().color = Color.
hplabel.SendMessage("SetTarget", this.gameObject);
hplabel.SendMessage("SetValue", damage);
_curHealth -=
damageTime = 0.2f;
Debug.Log("enemyondamage");
/// &summary&
/// 受到伤害
/// &/summary&
/// &param name="damage"&&/param&
public void EnemyOnDamageFOW(int damage)
if (CurrentDamage &= 0)
EnemyOnDamage(damage);
CurrentDamage++;
/// &summary&
/// 生命限制事件
/// &/summary&
public void EnemyVital()
#region 生命溢出限制
if (_curHealth & _maxHealth)
_curHealth = _maxH
if (_curHealth &= 0)
_curHealth = 0;
_isAlive = false;
#endregion
/// &summary&
/// 子类调用的通用事件
/// &/summary&
public void ChildrenUpdataALL()
FOWtime -= Time.deltaT
if (FOWtime &= 0)
CurrentDamage = 0;
FOWtime = 3;
damageTime -= Time.deltaT
DeathEvent();
EnemyVital();
ArmorPiercingCheck();
/// &summary&
/// 死亡事件 渐渐下落 然后销毁
/// &/summary&
public new void DeathEvent()
if (!_isAlive)
//碰撞器false
cc.enabled = false;
PlayDeathActionOnce();
this.transform.Translate(0, -0.002f, 0);
DeadDestoryTime -= Time.deltaT
if (DeadDestoryTime &= 0)
SenceSetCount();
Destroy(this.gameObject);
if (DropCount &= 0)//死亡后设置一次掉落
bossai.RushEffect.SetActive(false);
//int RandKey = Random.Range(1, 10);
//if (RandKey &= DropRange)//判断血瓶掉落几率
if (EnemyDrop)//掉落宝箱
StartCoroutine(dropbox());
if (m_GoldExp)
//DropGold();
DropCount++;
IEnumerator dropbox()
yield return new WaitForSeconds(1.5f);
Vector3 m_position = new Vector3(transform.position.x, transform.position.y, transform.position.z + 2.5f);
GameObject go = Instantiate(EnemyDrop, m_position,Quaternion.Euler(0,-135,0) ) as GameO
go.GetComponent&BossBox&().SetDurgList(GameManager.GetInstance().BossBoxRandom(BoxDropArray));
//switch (m_EnemyState)
case EnemyType.EnemyBomb:
case EnemyType.EnemyShot:
case EnemyType.EnemyFight:
case EnemyType.EnemyAOE:
case EnemyType.BossOne:
go.GetComponent&BossBox&().SetDurgList(GameManager.GetInstance().BossBoxRandom(GameManager.BossOneArray));
case EnemyType.BossTwo:
go.GetComponent&BossBox&().SetDurgList(GameManager.GetInstance().BossBoxRandom(GameManager.BossTwoArray));
case EnemyType.BossThree:
go.GetComponent&BossBox&().SetDurgList(GameManager.GetInstance().BossBoxRandom(GameManager.BossThreeArray));
case EnemyType.BossFour:
go.GetComponent&BossBox&().SetDurgList(GameManager.GetInstance().BossBoxRandom(GameManager.BossFourArray));
case EnemyType.BossFive:
go.GetComponent&BossBox&().SetDurgList(GameManager.GetInstance().BossBoxRandom(GameManager.BossFiveArray));
case EnemyType.Null:
case EnemyType.Enemylair:
private void PlayDeathActionOnce()
if (i &= 0)
MyAnimation.Play("daying");
if (MyAnimation["daying"].time & MyAnimation["daying"].length - 0.1f)
MyAnimation.Stop();
BOSS行为代码:
1 using UnityE
2 using System.C
4 public enum BossState {
idle,//idel
walk,//walk
bullet1,//bullet1
bullet2,//bullet2
attack1,//stabbing
attack2,//stright
attack3,//
17 public enum BossAttackState
Step1,//攻击阶段1 :发射弹幕1 两轮
Step2,//追逐主角 20秒标记 追上主角就肉搏打击 然后发射弹幕1 两轮 ,循环3次后进入Step3
Step3,//发射弹幕2 更改弹幕发射为随机 进入Step1 重置Step2count
25 public class BossAi : MonoBehaviour {
public GameObject Bullet1;//弹幕1
public GameObject Bullet2;//弹幕2
public BossState BossST = BossState.
public GameObject RushE
public int Step2count = 0;
public bool BulletRandom = false;
public float Step2TimerCD = 20;
public float Step2Timer = 0;
public Transform m_GunP
public BossAttackState m_BossAttackState = BossAttackState.Step1;
private AudioManager m_audioM
void Awake()
m_audioManager = GameObject.FindGameObjectWithTag ("Audio").GetComponent&AudioManager& ();
/// &summary&
/// 释放弹幕1
/// &/summary&
public void Bullet1Attack()
GameObject go = Instantiate(Bullet1, new Vector3(this.transform.position.x, this.transform.position.y + 1.0f, this.transform.position.z), this.transform.rotation) as GameO
//go.GetComponent&bulletOne&().Boss = this.gameO
BossST = BossState.bullet1;
/// &summary&
/// 释放弹幕1 通过事件添加
/// &/summary&
public void Bullet3Attack()
Debug.Log ("=========枪发子弹===========");
m_audioManager.Boss1_ZiDan ();
GameObject go = Instantiate(Bullet1, new Vector3(m_GunPoint.position.x, m_GunPoint.position.y, m_GunPoint.position.z), this.transform.rotation) as GameO
//go.GetComponent&bulletOne&().Boss = this.gameO
BossST = BossState.bullet1;
/// &summary&
/// 释放弹幕2
/// &/summary&
public void Bullet2Attack()
Debug.Log ("=========发射火焰===========");
m_audioManager.Boss1_HuoYan ();
GameObject go = Instantiate(Bullet2, new Vector3(this.transform.position.x,this.transform.position.y,this.transform.position.z),this.transform.rotation) as GameO
//go.transform.LookAt(player.transform.position);
BossST = BossState.bullet2;
private float _maxHealth = 1;
//最大生命值
private float _curHealth = 1;
//当前生命值
public Transform MyT
private bool _isAlive = true; //怪物是否活着
private float _moveS
//怪物移动速度
private int _rotationS
//怪物旋转速度
GameO//玩家
public Transform _//目标
public CharacterC
Animation MyA
int bulletonecount = 0;//判断是否发射弹幕1次
public NavMeshA//用于存储Robot身上附带的NavMeshAgent组件
public float BulletOneD
// Use this for initialization
void Start()
RushEffect.SetActive(false);
enemy = this.GetComponent&Enemy&();
if (enemy.m_EnemyState == EnemyType.BossOne)
BulletOneDelay = 6.0f;
BulletOneDelay = 3.0f;
//从enemy那初始化所有数据
_maxHealth = enemy._maxH
_curHealth = enemy._curH
_isAlive = enemy._isA
_moveSpeed = enemy._moveS
_rotationSpeed = enemy._rotationS
player = GameObject.FindGameObjectWithTag("Player");
_target = player.
cc = this.GetComponent&CharacterController&();
MyTransform = this.
MyAnimation = this.
nma = GetComponent&NavMeshAgent&();
nma.SetDestination(_target.position);//设置目的地位射线与平面的碰撞点
if (enemy.m_EnemyState == EnemyType.BossOne)
AnimationEvent evt = new AnimationEvent();
evt.time = 0.3f;
evt.functionName = "Bullet3Attack";
animation.GetClip("bullet1").AddEvent(evt);
public float BulletOneCD = 10.0f;//弹幕1冷却时间
private float BulletOneTimer = 0.0f;//弹幕1时间累加器
public float BulletTwoCD = 15.0f;//弹幕2冷却时间
private float BulletTwoTimer = 0.0f;//弹幕2时间累加器
public float RushCD = 8.0f; //野蛮冲撞冷却时间
private float RushTimer = 7.0f;//野蛮冲撞时间累加器
public float RushAttackDistance = 10.0f;//野蛮冲撞判断距离,在此距离内使用野蛮冲撞
public float SkillConflictCD = 5.0f;
//不同技能的使用间隔
private float SkillConflictTimer = 0.0f;
//判断是否能使用技能的时间累加器
void OnGUI()
//GUI.Box(new Rect(111, 111, 111, 50), Step2Timer.ToString()+" "+Step2count.ToString());
/// &summary&
/// 弹幕一攻击协同
/// &/summary&
/// &returns&&/returns&
IEnumerator StepOneC()
if (enemy.m_EnemyState == EnemyType.BossOne)
ContUsed = true;
MustFackToPlayer = true;
yield return new WaitForSeconds(0.01f);
_moveSpeed = 0;
yield return new WaitForSeconds(0.01f);
BossST = BossState.bullet1;
//Bullet1Attack();
yield return new WaitForSeconds(BulletOneDelay);
//Bullet1Attack();
yield return new WaitForSeconds(BulletOneDelay);
MustFackToPlayer = true;//冲撞的时候不能面向玩家
_moveSpeed = enemy._moveS
BossST = BossState.
ContUsed = false;
yield break;
ContUsed = true;
MustFackToPlayer = true;
yield return new WaitForSeconds(0.01f);
_moveSpeed = 0;
yield return new WaitForSeconds(0.01f);
Bullet1Attack();
yield return new WaitForSeconds(BulletOneDelay);
Bullet1Attack();
yield return new WaitForSeconds(BulletOneDelay);
MustFackToPlayer = true;//冲撞的时候不能面向玩家
_moveSpeed = enemy._moveS
BossST = BossState.
ContUsed = false;
yield break;
/// &summary&
/// 弹幕一攻击协同
/// &/summary&
/// &returns&&/returns&
IEnumerator StepThreeC()
ContUsed = true;
yield return new WaitForSeconds(8.01f);
_moveSpeed = 0;
Bullet2Attack();
yield return new WaitForSeconds(3.0f);
_moveSpeed = enemy._moveS
BossST = BossState.
m_BossAttackState = BossAttackState.Step2;
Step2Timer = 0;
ContUsed = false;
yield break;
/// &summary&
/// 弹幕一攻击协同
/// &/summary&
/// &returns&&/returns&
IEnumerator bullteOneAttackC()
if (enemy.m_EnemyState == EnemyType.BossOne)
ContUsed = true;
MustFackToPlayer = true;
yield return new WaitForSeconds(0.01f);
_moveSpeed = 0;
yield return new WaitForSeconds(0.01f);
BossST = BossState.bullet1;
//Bullet1Attack();
yield return new WaitForSeconds(BulletOneDelay);
MustFackToPlayer = true;//冲撞的时候不能面向玩家
_moveSpeed = enemy._moveS
BossST = BossState.
ContUsed = false;
yield break;
ContUsed = true;
MustFackToPlayer = true;
yield return new WaitForSeconds(0.01f);
_moveSpeed = 0;
yield return new WaitForSeconds(0.01f);
Bullet1Attack();
yield return new WaitForSeconds(BulletOneDelay);
MustFackToPlayer = true;//冲撞的时候不能面向玩家
_moveSpeed = enemy._moveS
BossST = BossState.
ContUsed = false;
yield break;
/// &summary&
/// 弹幕一攻击协同
/// &/summary&
/// &returns&&/returns&
IEnumerator bullteOneAttackC1()
if (enemy.m_EnemyState == EnemyType.BossOne)
ContUsed = true;
MustFackToPlayer = true;
yield return new WaitForSeconds(0.01f);
_moveSpeed = 0;
yield return new WaitForSeconds(0.01f);
BossST = BossState.bullet1;
//Bullet1Attack();
yield return new WaitForSeconds(BulletOneDelay);
MustFackToPlayer = true;//冲撞的时候不能面向玩家
_moveSpeed = enemy._moveS
BossST = BossState.
ContUsed = false;
yield break;
ContUsed = true;
MustFackToPlayer = true;
yield return new WaitForSeconds(0.01f);
_moveSpeed = 0;
yield return new WaitForSeconds(0.01f);
Bullet1Attack();
yield return new WaitForSeconds(BulletOneDelay);
MustFackToPlayer = true;//冲撞的时候不能面向玩家
_moveSpeed = enemy._moveS
BossST = BossState.
ContUsed = false;
yield break;
/// &summary&
/// 弹幕2攻击协同
/// &/summary&
/// &returns&&/returns&
IEnumerator bullteTwoAttackC()
ContUsed = true;
_moveSpeed = 0;
yield return new WaitForSeconds(0.01f);
MustFackToPlayer = false;
Bullet2Attack();
yield return new WaitForSeconds(3.0f);
MustFackToPlayer = true;
_moveSpeed = enemy._moveS
BossST = BossState.
ContUsed = false;
yield break;
/// &summary&
/// 弹幕一攻击协同
/// &/summary&
/// &returns&&/returns&
IEnumerator bullteTwoAttackC1()
ContUsed = true;
MyTransform.LookAt(player.transform.position);
_moveSpeed = 0;
yield return new WaitForSeconds(0.01f);
Bullet2Attack();
MustFackToPlayer = false;
yield return new WaitForSeconds(3.0f);
MustFackToPlayer = true;
_moveSpeed = enemy._moveS
BossST = BossState.
ContUsed = false;
yield break;
public float Attack1CD = 5.0f;//肉搏打击冷却时间
private float Attack1Timer = 0.0f;//肉搏打击时间累加器
/// &summary&
/// 肉搏打击协同
/// &/summary&
/// &returns&&/returns&
IEnumerator BossAttack1C()
ContUsed = true;
_moveSpeed = 0;
Debug.Log("Rush技能事件1");
yield return new WaitForSeconds(0.01f);
BossST = BossState.attack1;
yield return new WaitForSeconds(0.51f);
m_audioManager.Boss1_Fight();
//播放boss1肉搏的声音
_target.SendMessage("OnDamage", 35);
_target.SendMessage("GetHit");
yield return new WaitForSeconds(1.0f);
_moveSpeed = enemy._moveS
BossST = BossState.
ContUsed = false;
yield break;
public bool Rushing = false;//是否在野蛮冲撞中
private int RushCount = 0;
private bool ContUsed = false;
/// &summary&
/// 野蛮冲撞协同
/// &/summary&
/// &returns&&/returns&
IEnumerator RushC()
Debug.Log("=======野蛮冲撞======");
RushEffect.SetActive(true);
BossST = BossState.
ContUsed = true;
_moveSpeed = 0;//速度置为0,不让移动
nma.Stop();
Debug.Log("Rush技能事件1");
yield return new WaitForSeconds(3.01f);
BossST = BossState.
MustFackToPlayer = false;//冲撞的时候不能面向玩家
Rushing = true;
yield return new WaitForSeconds(1.5f);
RushEffect.SetActive(false);
Rushing = false;
_moveSpeed = 0;//速度置为0,不让移动
BossST = BossState.
yield return new WaitForSeconds(1.0f);
MustFackToPlayer = true;//接触不能面向玩家
_moveSpeed = enemy._moveS//
BossST = BossState.
nma.SetDestination(_target.position);
RushCount = 0;//置为空
ContUsed = false;
yield break;
private bool MustFackToPlayer = true;
/// &summary&
/// 设置目标
/// &/summary&
public void SetTarget(GameObject go)
_target = go.
float DistanceWithP
public float SecondMode = 0.5f;
// Update is called once per frame
void Update () {
PlayerAnimaFromState();
_curHealth = enemy._curH
_isAlive = enemy._isA
m_EnemyOnChaos = enemy.m_EnemyOnC
//更新BOSS状态
m_EnemyOnFire = enemy.m_EnemyOnF
m_EnemyOnFrozen = enemy.m_EnemyOnF
nma.speed = _moveS//将速度与enemy类中速度匹配
if (_isAlive)
//如果BOSS活着
if (m_EnemyOnFrozen)//如果在冰冻状态
nma.speed = 0;
enemy.FrozenTimer += Time.deltaT//冰冻时间累加器
if (enemy.FrozenTimer &= enemy.FrozenTimeCD)
enemy.m_EnemyOnFrozen = false;
enemy.FrozenTimer = 0;
nma.speed = _moveSpeed * (1 - 0.2f * enemy.SlowDownLV);
if (enemy.m_EnemyOnSlowDown)//减速
enemy.m_SlowDownTimer += Time.deltaT
if (enemy.m_SlowDownTimer &= enemy.m_SlowDownCD)
enemy.m_EnemyOnSlowDown = false;
enemy.m_SlowDownTimer = 0.0f;
enemy.SlowDownLV = 0;
if (m_EnemyOnChaos)//如果在混乱状态
OnChaos();//进入混乱
if (!_target)
//如果目标为空 则设置目标为玩家
SetTarget(player);
Debug.DrawLine(_target.position, MyTransform.position, Color.red);//画线
//BOSS和玩家的距离
DistanceWithPlayer = Vector3.Distance(_target.transform.position, transform.position);
Attack1Timer += Time.deltaT
//打击累加器
BulletOneTimer += Time.deltaT
//子弹1累加器
BulletTwoTimer += Time.deltaT
//子弹2累加器
SkillConflictTimer += Time.deltaT
//两次弹幕时间间隔累加器
if (_curHealth / _maxHealth &= SecondMode)
//(血量&=10%)//血量大于10% 启用远程模式
if (MustFackToPlayer)
FaceToTarget(_target.gameObject);
//面向主角;
nma.SetDestination(_target.position);//设置目标点
switch (m_BossAttackState)
case BossAttackState.Step1:
nma.speed = 0.0f;
StartCoroutine(StepOneC());
//第一步协同
m_BossAttackState = BossAttackState.Step2;
Step2count = 0;
case BossAttackState.Step2:
if (Step2count&=3)
m_BossAttackState = BossAttackState.Step3;
Step2Timer += Time.deltaT
if (Step2Timer &= Step2TimerCD)//
if (DistanceWithPlayer &= 3.5f)//(与玩家的距离小于1)
if (Attack1Timer &= Attack1CD && !ContUsed)
StartCoroutine(BossAttack1C());
Attack1Timer = 0;
Step2count++;
Step2Timer = 0;
Debug.Log("进行一次肉搏打击,将主角击退;");
if (BulletRandom)
int i = Random.Range(1, 3);
switch (i)
if (!ContUsed)
StartCoroutine(bullteOneAttackC1());
//发射弹幕1;//协同
if (!ContUsed)
StartCoroutine(bullteTwoAttackC1());
//发射弹幕2;//协同
if (!ContUsed)
StartCoroutine(bullteOneAttackC1());
//发射弹幕1;//协同
Step2count++;
Step2Timer = 0;
case BossAttackState.Step3:
StartCoroutine(StepThreeC());
//发射弹幕2;//协同
Step2count = 0;
BulletRandom = true;
Step2Timer = 0;
m_BossAttackState = BossAttackState.Step2;
case BossAttackState.Null:
else//血量小于10%的时候, //随机使用3种攻击方式;
if (MustFackToPlayer)
FaceToTarget(_target.gameObject);
//面向主角;
if (_moveSpeed != 0)
//nma.SetDestination(_target.position);//设置目的地位射线与平面的碰撞点
//nma.Stop();
if (Rushing)//正在野蛮冲撞中
nma.Stop();
nma.speed = 0;
Vector3 movement1 = transform.forward * 5.0f * 1.5f;
movement1 *= Time.deltaT
cc.Move(movement1);
if (DistanceWithPlayer &= 2.5f)//如果撞到玩家则对玩家造成伤害
if (RushCount &= 0)
_target.SendMessage("OnDamage", 35);
_target.SendMessage("GetHit");
RushCount++;
SkillConflictTimer = 0.0f;
nma.SetDestination(_target.position);//设置目的地位射线与平面的碰撞点
RushTimer += Time.deltaT
//累加冷却时间
if (DistanceWithPlayer &= 2.5f)
//(判断是否与玩家距离大于1)
if (RushTimer &= RushCD && SkillConflictTimer &= SkillConflictCD && !ContUsed)//if (野蛮冲撞CD冷却,并且在野蛮冲撞距离内)
StartCoroutine(RushC());//
使用野蛮冲撞,并且僵直5秒;//协同
RushTimer = 0;
重置野蛮冲撞CD;
SkillConflictTimer = 0;//重置时间 使肉搏打击不冲突
if (SkillConflictTimer &= SkillConflictCD && MustFackToPlayer && (!ContUsed))
//判断是否可以发射弹幕
if (BulletOneTimer &= BulletOneCD)
//(弹幕1冷却时间到)
int i = Random.Range(1,3);
switch (i)
StartCoroutine(bullteOneAttackC());
//发射弹幕1;//协同
this.transform.LookAt(_target);
StartCoroutine(bullteTwoAttackC());
//发射弹幕2;//协同
this.transform.LookAt(_target);
BulletOneTimer = 0;
//重置弹幕冷却时间;
SkillConflictTimer = 0;
//重置可发射弹幕冷却时间
if (DistanceWithPlayer &= 3.5f)//(与玩家的距离小于1)
int yes=0;
if (SkillConflictTimer &= SkillConflictCD)
if (Attack1Timer &= Attack1CD && (!ContUsed))
StartCoroutine(BossAttack1C());
Attack1Timer = 0;
SkillConflictTimer = 0;
Debug.Log("进行一次肉搏打击,将主角击退;");
//进行一次肉搏打击,将主角击退;//需要在主角那设置一个后退函数
_moveSpeed = 0.0f;
nma.Stop();
private bool m_EnemyOnChaos = false;//混乱状态
private float ChaosTimeCD = 5;
private float ChaosTimer = 0;
private bool m_EnemyOnFrozen = false;//冰冻状态
private bool m_EnemyOnFire = false;//燃烧状态
private float ChaosWalkCD = 1.0f;
private float ChaosWalkTimer = 1.0f;
/// &summary&
/// 判断混乱状态 放到update里面
/// &/summary&
public void OnChaos()
//如果混乱中
if (enemy.m_EnemyOnChaos)
ChaosTimer += Time.deltaT//开始计时
SetChaosWalkPoint();//每2秒更换一次跑的地点
if (ChaosTimer &= ChaosTimeCD)//混乱时间到
ChaosTimer = 0;//时间制空
ChaosWalkTimer = 0.0f;
enemy.m_EnemyOnChaos = false;//将混乱状态置F
/// &summary&
/// 每2秒设置一次混乱状态奔跑点
/// &/summary&
void SetChaosWalkPoint()
ChaosWalkTimer += Time.deltaT
if (ChaosWalkTimer &= ChaosWalkCD)
nma.SetDestination(new Vector3(this.transform.position.x + Random.Range(-3.0f, 3.0f), this.transform.position.y, this.transform.position.z + Random.Range(-3.0f, 3.0f)));//设置目的地
ChaosWalkTimer = 0;
/// &summary&
/// 面向目标
/// &/summary&
/// &param name="target"&&/param&
public void FaceToTarget(GameObject target)
//朝向玩家 y轴不变
MyTransform.rotation = Quaternion.Slerp(MyTransform.rotation, Quaternion.LookRotation(new Vector3(target.transform.position.x, 0, target.transform.position.z) - new Vector3(MyTransform.position.x, 0, MyTransform.position.z)), _rotationSpeed * Time.deltaTime);
/// &summary&
/// 从状态更新动画
/// &/summary&
public void PlayerAnimaFromState()
if (_isAlive)
if (_moveSpeed == enemy._moveSpeed)
BossST = BossState.
if (Rushing)
BossST = BossState.
switch (BossST)
case BossState.idle:
MyAnimation.Play("idel");
case BossState.Rushing:
MyAnimation.Play("Rushing");
case BossState.walk:
MyAnimation["run"].speed = 1.0f;
MyAnimation.Play("run");
case BossState.run:
MyAnimation["run"].speed = 1.8f;
MyAnimation.Play("run");
case BossState.bullet1:
if (enemy.m_EnemyState == EnemyType.BossOne)
MyAnimation["bullet1"].speed = 0.8f;
MyAnimation.Play("bullet1");
if (MyAnimation["bullet1"].time & MyAnimation["bullet1"].length - 0.1f)
//BossST = BossState.
MyAnimation["bullet1"].speed = 0.8f;
MyAnimation.Play("bullet1");
if (MyAnimation["bullet1"].time & MyAnimation["bullet1"].length - 0.1f)
//BossST = BossState.
case BossState.bullet2:
MyAnimation["bullet2"].speed = 0.5f;
MyAnimation.Play("bullet2");
if (MyAnimation["bullet2"].time & MyAnimation["bullet2"].length - 0.1f)
//BossST = BossState.
case BossState.attack1:
MyAnimation["attack1"].speed = 1.5f;
MyAnimation.Play("attack1");
if (MyAnimation["attack1"].time & MyAnimation["attack1"].length - 0.1f)
BossST = BossState.
case BossState.attack2:
MyAnimation.Play("attack1");
if (MyAnimation["attack1"].time & MyAnimation["attack1"].length - 0.1f)
MyAnimation.Stop();
case BossState.attack3:
MyAnimation.Play("attack1");
case BossState.daying:
MyAnimation["daying"].speed = 0.1f;
MyAnimation.Play("daying");
if (MyAnimation["daying"].time & MyAnimation["daying"].length - 0.1f)
MyAnimation.Stop();
阅读(...) 评论()

我要回帖

更多关于 unity3d碰撞检测代码 的文章

 

随机推荐