前边我们创建了一个静止的敌人,现在我们给它基本的行动能力,障碍检测能力
脚本 WanderingAI
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WanderingAI : MonoBehaviour {
private static float speed = 3.0f;
public float obstacleRange = 5.0f;
private bool _alive;
// Use this for initialization
void Start () {
_alive = true;
}
public static void setSpeed() {
speed++;
if (speed > 10) {
speed = 10;
}
}
public void setAlive(bool alive) {
_alive = alive;
}
// Update is called once per frame
void Update () {
if (_alive)
{
transform.Translate(0, 0, speed * Time.deltaTime);
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if (Physics.SphereCast(ray, 0.75f, out hit))
{
if (hit.distance < obstacleRange)
{
float angle = Random.Range(-110, 110);
transform.Rotate(0, angle, 0);
}
}
}
}
}
敌人被击杀后,我们移除死亡的敌人,通过预设体创建新的敌人。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SceneController : MonoBehaviour {
[SerializeField]
private GameObject enemyPrefab;
private GameObject _enemy;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (_enemy == null) {
_enemy = Instantiate(enemyPrefab) as GameObject;
int idx = Random.Range(1, 5);
int siteX = 0;
int siteZ = 0;
switch (idx) {
case 1:
siteX = Random.Range(-5, -60);
siteZ = Random.Range(-15,20);
break;
case 2:
siteX = Random.Range(-15, -65);
siteZ = Random.Range(-35, -60);
break;
case 3:
siteX = Random.Range(15, 60);
siteZ = Random.Range(-35, -60);
break;
case 4:
siteX = Random.Range(60, -60);
siteZ = Random.Range(45, 70);
break;
default:
siteX = Random.Range(-5, -60);
siteZ = Random.Range(-15, 20);
break;
}
_enemy.transform.position = new Vector3(siteX, 0.5f, siteZ);
float angle = Random.Range(0,360);
_enemy.transform.Rotate(0,angle,0);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ReacticeTarget : MonoBehaviour {
public void ReactToHit() {
WanderingAI behavior = GetComponent<WanderingAI>();
if (behavior != null) {
WanderingAI.setSpeed();
behavior.setAlive(false);
}
StartCoroutine(Die());
}
private IEnumerator Die() {
this.transform.Rotate(-75, 0, 0);
yield return new WaitForSeconds(1.5f);
Destroy(this.gameObject);
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
通过预设体,我们让敌人具有攻击能力
if (hitObject.GetComponent<PlayerCharacter>())
{
if (_fireball == null)
{
_fireball = Instantiate(fireballPrefab) as GameObject;
_fireball.transform.position = transform.TransformPoint(Vector3.forward * 1.5f);
_fireball.transform.rotation = transform.rotation;
}
}
击中玩家对象处理
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCharacter : MonoBehaviour {
private int _health;
// Use this for initialization
void Start () {
_health = 5;
}
// Update is called once per frame
void Update () {
}
public void Hurt(int damage) {
_health -= damage;
Debug.Log("Health: " + _health);
}
}
这里给玩家对象5点血,每次击中减少一点。
致此FPS简单的射击游戏就实现完成了。
游戏图示、游戏下载、源码下载http://blog.youkuaiyun.com/d276031034/article/details/56016801