unity项目:坦克大战:敌人预设体的代码
·
敌人预设体添加两个脚本。

第一个是敌方坦克AI
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class emptyIsKill : MonoBehaviour {
public float moveSpeed=10;
public float rotateSpeed=10;
private Transform mytransform;
private GameObject player;
[Header("发射炮弹间隔")]
public float paoDanSpeed_Time = 5f;
[Header("炮弹的预设体")]
public GameObject bulletPrefab;
[Header("被发现预设体")]
public GameObject isFind;
private GameObject xinHao;
private bool xinHaoFlag = true;
private Vector3 moveFangXiang=new Vector3 (1,0,0);
private GameObject blt;
private Transform firePoint;
private Transform faXianPoint;
void Awake()
{
mytransform = this.transform;
}
void Start()
{
player = GameObject.FindWithTag("Player");
firePoint = transform.Find("FirePoint");
faXianPoint = transform.Find("FaXianPoint");
//moveFangXiang = new Vector3(Random.Range(0, 10) / 10, 0, Random.Range(0, 10) / 10);
}
void Update () {
if ((mytransform.position - player.transform.position).sqrMagnitude < 3600)
{
mytransform.LookAt(player.transform);
transform.Rotate(Vector3.down * 95);
}
else
{
transform.position +=transform.right * moveSpeed;
}
if (xinHaoFlag == true&& (mytransform.position - player.transform.position).sqrMagnitude < 3600)
{
xinHao = Instantiate(isFind);
xinHao.transform.localScale=new Vector3(3,3,3);
xinHao.transform.position = faXianPoint.position;
xinHaoFlag = false;
}
if (xinHao)
{
xinHao.transform.position=faXianPoint.position;
}
Destroy(xinHao, 3f);
paoDanSpeed_Time -= Time.deltaTime;
if (paoDanSpeed_Time < 0)
{
blt = Instantiate(bulletPrefab,firePoint.position,Quaternion.identity);
blt.transform.localScale = new Vector3(1, 1, 1);
blt.GetComponent<Bullet>().moveDir = mytransform.right;
blt.GetComponent<Bullet>().moveSpeed = 3f;
paoDanSpeed_Time = 4f;
}
Destroy(blt, 3f);
}
void OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.gameObject.tag == "paoDan")
{
Destroy(xinHao);
Debug.Log("awsl");
Destroy(this.gameObject);
}
}
}
第二个控制敌人移动和转向
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class emptyMove : MonoBehaviour {
public float moveSpeed=0.2f;
public Vector3 moveFangXiang;
void Start()
{
moveFangXiang = transform.right;
}
void Update () {
transform.position += moveFangXiang * moveSpeed;
}
void OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.gameObject.tag == "Terrain")
{
transform.Rotate(new Vector3(0 , Random.Range(0, 180), 0));
}
}
}
更多推荐



所有评论(0)