using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
*强调内容*
public class FireBullet : MonoBehaviour {
public GameObject bulletdemo;//传一个粒子效果预制体
private GameObject obj;//接收克隆的对象
private Collider[] searcharr;//球形探测碰撞器
// Use this for initialization
void Start ()
{
}
float time;
// Update is called once per frame
void Update () {
time += Time.deltaTime;
searcharr = Physics.OverlapSphere(transform.position, 10);//transform.position作为圆心,10代表探测范围
if (searcharr.Length > 0)
{
foreach (var item in searcharr)
{
if (item.gameObject.CompareTag("soldier"))
{
Queue<Collider> queue = new Queue<Collider>();
queue.Enqueue(item);
if (Vector3.Distance(queue.Peek().transform.position, transform.position) > 10)
{
queue.Dequeue();
}
else
{
if (time > 1)
{
time -= 1;
obj = GameObject.Instantiate(bulletdemo, new Vector3(transform.position.x, 9, transform.position.z), transform.rotation) as GameObject;
obj.transform.LookAt(queue.Peek().transform);//使炮塔始终盯队列的头一个(再加上炮弹移动实现攻击)
}
}
}
}
}
}
}
********************************************
using UnityEngine;
using System.Collections;
public class Bullet : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate(Vector3.forward * Time.deltaTime * 40, Space.Self);
Destroy(gameObject, 3);//40是给泡弹一个速度,这里写死了可以定义变量从外部控制
}
void OnTriggerEnter(Collider other)//如果碰到小兵,小兵死,自身销毁
{
if (other.gameObject.CompareTag("soldier"))
{
Destroy(gameObject);
Destroy(other.gameObject);
}
}
}
实现炮塔打兵
最新推荐文章于 2019-05-31 15:37:16 发布