}
2、 //控制动画 if (h != 0 || v != 0) { anim.SetBool("Move", true); } else { anim.SetBool("Move", false); }
********************* 控制射击 **************************
void Update () { timer+=Time.deltaTime; if (timer > 1 / shootRate) { timer -= 1 / shootRate; Shoot(); } } void Shoot() { light.enabled = true; particleSystem.Play(); this.lineRenderer.enabled = true; lineRenderer.SetPosition(0, transform.position); Ray ray = new Ray(transform.position, transform.forward); RaycastHit hitInfo; if (Physics.Raycast(ray, out hitInfo)) { lineRenderer.SetPosition(1, hitInfo.point); //判断当前的射击有没有碰撞到敌人 if (hitInfo.collider.tag == Tags.enemy) { hitInfo.collider.GetComponent<EnemyHealth>().TakeDamage(attack,hitInfo.point); } } else { lineRenderer.SetPosition(1, transform.position + transform.forward * 100); } audio.Play(); Invoke("ClearEffect", 0.05f); }
战斗场景
1. 设置敌人跟随主角,需要用到导航组件。“windows--navigation”,再点击“Back”按钮烘焙一下。则整个绿色的部分就是导航的部分。 也就是利用导航网格来追击主角的运动, 给兔子添加一个“Nav Mesh Agent” 组件。 2. 再添加一个脚本“Enemy Move”。 ******************************************* 追击主角 ******************************************** public class EnemyMove : MonoBehaviour { private NavMeshAgent agent; private Transform player; void Awake () { agent = this.GetComponent<NavMeshAgent> (); } void Start () { player = GameObject.FindGameObjectWithTag (Tags.play).transform; } void Update () { if (Vector3.Distance(transform.position, player.position) < 1.5f) { agent.Stop(); anim.SetBool("Move", false); } else { agent.SetDestination(player.position); // anim.SetBool("Move", true); } ********** ************* 敌人血值 ******************************************** 如果在敌人身上挂载两个音乐? private AudioSource audio; //受到伤害的声音 audio = this.GetComponent<AudioSource> (); audio.Play (); public AudioClip deathClip; //死亡时声音,使用静态方法
AudioSource.PlayClipAtPoint (deathClip, transform.position, 1.0f); ******************************************* 自动生成敌人 ******************************************** 1. 创建一个空的游戏物体,reset一下。命名为Spawn。 2. 在其下创建几个空的子物体,分别命名为三个敌人的名称。分别把位置拉倒生成的地方。分别设置标签 3. public class Spawn : MonoBehaviour { public GameObject enemyPrefab; public float spawntime = 3; private float time = 0; void Update () { time += Time.deltaTime; //计时 if (time >= spawntime) { //如果到达生成时间 time -= spawntime; SpawnEnemy (); } } void SpawnEnemy(){ //生成语句,不进行旋转 GameObject.Instantiate(enemyPrefab, transform.position, transform.rotation); } } 难度增加: void Start(){ InvokeRepeating("ACC", 1, 1); } void ACC(){ spawntime -= 0.05f; //每隔1s就把生成敌人的时间减少 }