Unity初级项目实战:官方宇宙飞机大战游戏(二)

二.设置玩家飞行器,控制玩家移动和限制范围,制作子弹,控制发射子弹
1.在Models文件夹里面找到vehicle_playerShip,拉进Scene面板,取名为Player,位置往上拉5米
Unity初级项目实战:官方宇宙飞机大战游戏(二)_第1张图片
2.给player加上Box碰撞器,同时在Player前方新建一个子物体,作为子弹生成的位置,每次发射子弹,在这里生成发射
Unity初级项目实战:官方宇宙飞机大战游戏(二)_第2张图片
3.然后在文件夹里找到飞船的引擎的粒子特效,拉到Player下作为子物体,调整位置,运行游戏可以看到效果
Unity初级项目实战:官方宇宙飞机大战游戏(二)_第3张图片
4.制作子弹预制体:在文件夹里面找到子弹预制体,然后拉到场景里,取名为Bullet,然后拉回我们自己的预制体文件夹:
Unity初级项目实战:官方宇宙飞机大战游戏(二)_第4张图片
5.控制飞船移动和移动范围:新建一个脚本,取名为:Player
然后脚本如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]//可显示
public class PlayerClampBounder//移动的X,Z最大范围或者最小范围类
{
    public float MaxX;
    public float MaxZ;
    public float MinX;
    public float MinZ;
}
public class Player : MonoBehaviour {

    public PlayerClampBounder Bounder;//边界范围
    public float MoveSpeed;//移动速度
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        MoveMent();
    }

    public void MoveMent()//控制移动的方法
    {
        float Horizontal = Input.GetAxis("Horizontal");//获取水平输入轴
        float Vertical = Input.GetAxis("Vertical");//获取垂直输入轴
        if (transform.position.x >= Bounder.MaxX && Horizontal > 0)//如果当前x位置大于等于边界范围,且还在向这个方向运动,也就是说还按下了向右,就让水平输入值为0,也就不会再继续向右
        {
            Horizontal = 0;
        }
        else if (transform.position.x <= Bounder.MinX && Horizontal < 0)//同理
        {
            Horizontal = 0;
        }
        else if (transform.position.z >= Bounder.MaxZ && Vertical > 0)
        {
            Vertical = 0;
        }
        else if (transform.position.z <= Bounder.MinZ && Vertical < 0)
        {
            Vertical = 0;
        }
        transform.Translate(Horizontal*MoveSpeed*Time.deltaTime, 0, Vertical*Time.deltaTime*MoveSpeed);//移动
    }
}

写完脚本挂到Player下,设置边界范围和速度
Unity初级项目实战:官方宇宙飞机大战游戏(二)_第5张图片
6.让子弹飞起来:新建一个脚本Bullet,让子弹发射出去,代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour {

    public float BulletMoveSpeed = 10;
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        BulletMove();
    }
    private void BulletMove()//移动的方法
    {
        transform.Translate(0, 0, BulletMoveSpeed * Time.deltaTime);
    }
}

7.发射子弹,在Player脚本里,新建一个方法,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]//可显示
public class PlayerClampBounder//移动的X,Z最大范围或者最小范围类
{
    public float MaxX;
    public float MaxZ;
    public float MinX;
    public float MinZ;
}
public class Player : MonoBehaviour {

    public PlayerClampBounder Bounder;//边界范围
    public float MoveSpeed;//移动速度

    public float ShotDuratime = 0.3f;//发射的间隔时间
    public GameObject BulletPrefab;//子弹的预制体,在unity进行赋值
    private Transform BulletPoint;//子弹生成的位置
    void Start () {
        BulletPoint = transform.Find("BulletPoint");//获取子物体BulletPoint
    }

    // Update is called once per frame
    void Update () {
        MoveMent();
        ShotBullet();
    }

    public void MoveMent()//控制移动的方法
    {
        float Horizontal = Input.GetAxis("Horizontal");//获取水平输入轴
        float Vertical = Input.GetAxis("Vertical");//获取垂直输入轴
        if (transform.position.x >= Bounder.MaxX && Horizontal > 0)//如果当前x位置大于等于边界范围,且还在向这个方向运动,也就是说还按下了向右,就让水平输入值为0,也就不会再继续向右
        {
            Horizontal = 0;
        }
        else if (transform.position.x <= Bounder.MinX && Horizontal < 0)//同理
        {
            Horizontal = 0;
        }
        else if (transform.position.z >= Bounder.MaxZ && Vertical > 0)
        {
            Vertical = 0;
        }
        else if (transform.position.z <= Bounder.MinZ && Vertical < 0)
        {
            Vertical = 0;
        }
        transform.Translate(Horizontal*MoveSpeed*Time.deltaTime, 0, Vertical*Time.deltaTime*MoveSpeed);//移动
    }
    private void ShotBullet()
    {
        ShotDuratime -= Time.deltaTime;
        if (ShotDuratime <= 0&&Input.GetMouseButton(0))//当时间到了且按下了鼠标左键
        {
            Instantiate(BulletPrefab, BulletPoint.position, Quaternion.identity);//生成子弹
            ShotDuratime = 0.3f;//重新设置间隔时间
        }

    }
}

进行简单赋值之后,运行,飞船便可以发射子弹,但是子弹会一直存在于场景内,所以下一节,我们会说到子弹的销毁。
Unity初级项目实战:官方宇宙飞机大战游戏(二)_第6张图片

你可能感兴趣的:(Unity开发,游戏开发)