Unity开发详解之预设体(6/6)


前边我们创建了一个静止的敌人,现在我们给它基本的行动能力,障碍检测能力

脚本 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.csdn.net/d276031034/article/details/56016801

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风晴03

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值