接着笔记五:
此次主要内容是:
- 点击怪物的时候发送子弹,子弹打中怪物的时候怪物血量才会减小
- 子弹数量视图
- 主角剩余血量视图
- 子弹夹,医药箱功能
1.先建立一个子弹:GameObject -> Create Other -> sphere ->更名:bullet
而后将球体缩小到必定的程度,给球体添加一个贴图,并给子弹添加刚体组件:
2.新建一个子弹预设,把上面作好的子弹拖到预设。
3.而后建立一个空的场景对象:GameObject -> Create Empty, 更名firePoint,这个对象的主要做用是保存
子弹的发射的起点,而且修改脚本:RoleMoveController,让这个空对象始终保持在主角(摄像机)
的前面:
//发射位置
private GameObject firePoint;
// Use this for initialization
void Start () {
firePoint = GameObject.Find("firePoint");
}
void Update () {
if (Input.GetKey (KeyCode.A)) {
this.transform.Translate(Vector3.left*Time.deltaTime*Common.MOVE_SPEED);
}
else if (Input.GetKey (KeyCode.D)) {
this.transform.Translate(Vector3.right*Time.deltaTime*Common.MOVE_SPEED);
}
else if (Input.GetKey (KeyCode.W)) {
this.transform.Translate(Vector3.forward*Time.deltaTime*Common.MOVE_SPEED);
}
else if (Input.GetKey (KeyCode.S)) {
this.transform.Translate(Vector3.back*Time.deltaTime*Common.MOVE_SPEED);
}
//让发射点始终保持在主角的前面
firePoint.transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + 3);
}
4.修改RoleBulletController,相应鼠标左键发射子弹,并在左下角实时显示剩余子弹数:
//受重力影响的子弹
public Rigidbody bullet;
//发射位置
private GameObject firePoint;
//受重力影响得子弹
public Rigidbody bullet;
//发射位置
private GameObject firePoint;
// Use this for initialization
void Start () {
role = GameObject.Find ("role");
firePoint = GameObject.Find("firePoint");
}
// Update is called once per frame
void Update () {
//点击鼠标左键
if (Input.GetMouseButtonDown(Common.MOUSE_BTN_LEFT) && bullets > 0)
{
//剩余子弹个数
bullets--;
//经过射线得到目标点
//Returns a ray going from camera through a screen point.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//Returns a point at distance units along the ray.
Vector3 target = ray.GetPoint(20);
//修改发射起点的朝向
firePoint.transform.LookAt(target);
//实例化子弹
Rigidbody clone = (Rigidbody)Instantiate(bullet, firePoint.transform.position, firePoint.transform.rotation);
//初始化子弹的方向速度
clone.velocity = target - firePoint.transform.position;
}
}
void OnGUI()
{
GUI.Label (new Rect (10, Screen.height - 30, 100, 50), "子弹个数 x"+bullets);
}
5.运行游戏,会发现,当子弹打中怪物的时候,怪物会后退,咱们不要这个选项,因此在选中怪物的
预设,选中其刚体组件,勾选
isKinematic属性,那么这个物体将只有碰撞属性不具有物理学属性。
6.接下来修改EnemyController,让怪物在与子弹发生碰撞以后才扣血:
修改原来方法OnMouseDown为OnCollisionEnter:
//怪物响应碰撞事件
void OnCollisionEnter(Collision collision)
{
if (blood > 0) {
isAttacked = true;
blood = blood - 5;
if (0 >= blood)
{
dead();
}
}
//子弹碰到怪物后消失
Destroy (collision.gameObject);
}
7.修改RoleBloodController,用来实时显示主角的剩余血量:
//初始血量
private int Blood = 100;
public void reduceBlood(int reduceNum)
{
Blood = Blood - reduceNum;
//失败的时候跳回菜单页面从新进入游戏
if (0 >= Blood)
{
Application.LoadLevel("MainScene");
SceneController.currentGameState = GameState.GAME_MENU;
}
}
void OnGUI()
{
GUI.Label (new Rect (10, Screen.height - 70, 100, 50), "剩余血量 x"+Blood);
}
8.接下来就是实现医药箱跟子弹夹功能,先把医药箱跟子弹夹作成预设,而后给它们分别添加
标签:Medicine和BulletBox, 而后在RoleBloodController中修改:
//场景中的医药包
private GameObject[] medicines;
// Use this for initialization
void Start () {
medicines = GameObject.FindGameObjectsWithTag ("Medicine");
}
// Update is called once per frame
void Update () {
for (int i= 0; i < medicines.Length; i++)
{
if(null == medicines[i]) continue;
//当医药箱与主角距离小于2的时候被主角拾取
if(Vector3.Distance(transform.position, medicines[i].transform.position) <= 2)
{
Blood = Blood + 50;
Destroy(medicines[i]);
medicines[i] = null;
}
}
}
子弹夹同理。
9.运行程序~!oh ye...终于作完. this
9.运行程序~!oh ye...终于作完。
10.导出程序: