Unity 怪物巡逻并攻击玩家C#代码笔记
2017-06-09 14:17
1026 查看
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class BringEnemyScript : MonoBehaviour {
public GameObject[] GoOnPatrolPoint;
NavMeshAgent enemynavmeshagent;
int count=1;
Animator EnemyAnimator;
bool startoroutine;
GameObject distance;
Vector3 distancePosition;
bool IsAttack;
// Use this for initialization
void Start () {
EnemyAnimator = GetComponent<Animator> ();
enemynavmeshagent= GetComponent<NavMeshAgent> ();
enemynavmeshagent.destination = GoOnPatrolPoint[0].transform.position;
}
// Update is called once per frame
void Update () {
Collider[] Players= Physics.OverlapSphere (this.transform.position, 9);
if(Players!=null){
foreach( Collider _AttackPlayer in Players){
if (_AttackPlayer.name != "humanoid_dying") {
IsAttack = false;
continue;
}
if (_AttackPlayer.name == "humanoid_dying") <
d6b6
span style="color:#333333;">{
IsAttack = true;
StopCoroutine ("ChangeDestination");
distance = _AttackPlayer.gameObject;
this.transform.LookAt (distance.transform.position);
}
if((_AttackPlayer.transform.position-this.transform.position).magnitude<1){
EnemyAnimator.SetBool ("Attack",true);
}
EnemyAnimator.SetBool ("ToRun",true);
enemynavmeshagent.destination=_AttackPlayer.transform.position;
}
if(enemynavmeshagent.remainingDistance==0&&startoroutine==false){
StartCoroutine ("ChangeDestination");
}
if(distance!=null&&(distance.transform.position - this.transform.position).magnitude >= 10&&!IsAttack){
EnemyAnimator.SetBool ("ToRun",false);
EnemyAnimator.SetBool ("Attack",false);
enemynavmeshagent.destination = distancePosition;
}
}
}
IEnumerator ChangeDestination(){
startoroutine = true;
EnemyAnimator.SetTrigger ("Idle");
yield return new WaitForSeconds (EnemyAnimator.GetCurrentAnimatorStateInfo(0).length);
this.transform.Rotate(Vector3.Lerp(new Vector3(0,0,0),new Vector3(0,180,0),0.3f));
enemynavmeshagent.destination = GoOnPatrolPoint[count%GoOnPatrolPoint.Length].transform.position;
count++;
distancePosition = enemynavmeshagent.destination;
startoroutine = false;
}
}
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class BringEnemyScript : MonoBehaviour {
public GameObject[] GoOnPatrolPoint;
NavMeshAgent enemynavmeshagent;
int count=1;
Animator EnemyAnimator;
bool startoroutine;
GameObject distance;
Vector3 distancePosition;
bool IsAttack;
// Use this for initialization
void Start () {
EnemyAnimator = GetComponent<Animator> ();
enemynavmeshagent= GetComponent<NavMeshAgent> ();
enemynavmeshagent.destination = GoOnPatrolPoint[0].transform.position;
}
// Update is called once per frame
void Update () {
Collider[] Players= Physics.OverlapSphere (this.transform.position, 9);
if(Players!=null){
foreach( Collider _AttackPlayer in Players){
if (_AttackPlayer.name != "humanoid_dying") {
IsAttack = false;
continue;
}
if (_AttackPlayer.name == "humanoid_dying") <
d6b6
span style="color:#333333;">{
IsAttack = true;
StopCoroutine ("ChangeDestination");
distance = _AttackPlayer.gameObject;
this.transform.LookAt (distance.transform.position);
}
if((_AttackPlayer.transform.position-this.transform.position).magnitude<1){
EnemyAnimator.SetBool ("Attack",true);
}
EnemyAnimator.SetBool ("ToRun",true);
enemynavmeshagent.destination=_AttackPlayer.transform.position;
}
if(enemynavmeshagent.remainingDistance==0&&startoroutine==false){
StartCoroutine ("ChangeDestination");
}
if(distance!=null&&(distance.transform.position - this.transform.position).magnitude >= 10&&!IsAttack){
EnemyAnimator.SetBool ("ToRun",false);
EnemyAnimator.SetBool ("Attack",false);
enemynavmeshagent.destination = distancePosition;
}
}
}
IEnumerator ChangeDestination(){
startoroutine = true;
EnemyAnimator.SetTrigger ("Idle");
yield return new WaitForSeconds (EnemyAnimator.GetCurrentAnimatorStateInfo(0).length);
this.transform.Rotate(Vector3.Lerp(new Vector3(0,0,0),new Vector3(0,180,0),0.3f));
enemynavmeshagent.destination = GoOnPatrolPoint[count%GoOnPatrolPoint.Length].transform.position;
count++;
distancePosition = enemynavmeshagent.destination;
startoroutine = false;
}
}
相关文章推荐
- 【Unity学习笔记】NPC朝向玩家的代码实现
- 实现敌人(怪物)的简单AI(自动巡逻、看到玩家攻击玩家、玩家离开恢复自动巡逻)
- C# DDOS 攻击工 代码
- 日泽IT笔记系列1-Beginning C# Objects 从概念到代码-1
- cocos2d-x系列笔记(9.1)---浅谈cocos2d-x使用函数指针实现委托(上)---通过c#代码理解委托
- C#笔记26: 与非托管代码交互操作
- Unity调用外接摄像头的实例代码(C#)
- [Eclipse笔记]Just for fun – 在Eclipse下编译和运行C#的代码
- C# 代码笔记
- c# 委托的学习笔记(附代码)
- 正则表达式笔记:提取C#代码中的中文信息,双引号,尖括号间的信息
- C#调用存储过程实现分页(个人代码笔记)
- 日泽IT笔记系列1-Beginning C# Objects 从概念到代码-2
- C#笔记32:FRAMEWORK安全性之代码访问安全和角色安全
- C#笔记26: 与非托管代码交互操作
- 【笔记】《C#高效编程改进C#代码的50个行之有效的办法》第1章C#语言习惯(1)--属性的特性以及索引器(SamWang)
- 『框架设计(第2版)CLR Via C#』学习笔记(002)——将托管代码合并到程序集
- C#学习笔记:13个代码注释的小技巧(转)
- 编写高质量C#代码学习笔记(2)
- 代码笔记:使用C#转移Wordpress博客到为知笔记或Evernote