Unity基础实践小项目(飞机大战)——四元数的运用

目录

飞机代码:

子弹代码:


主要提供飞机代码以及子弹代码。其中飞机代码中提供了子弹的发射数量等等发射逻辑。子弹代码中包含子弹运动逻辑。以下代码仅供参考。

飞机代码:

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

public enum E_FireType
{
    One,
    Two,
    Three,
    Round
}
public class Plan : MonoBehaviour
{
    private E_FireType nowType= E_FireType.One;
    public GameObject bullet;
    //设置子弹数量
    public int roundNum = 4;

    // Update is called once per frame
    void Update()
    {
        //设置一个键盘选择输入
        if(Input.GetKeyDown(KeyCode.Alpha1))
        {
            nowType= E_FireType.One;
        }
        else if(Input.GetKeyDown(KeyCode.Alpha2))
        {
            nowType= E_FireType.Two;
        }
        else if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            nowType= E_FireType.Three;
        }
        else if(Input.GetKeyDown(KeyCode.Alpha4))
        {
            nowType = E_FireType.Round;
        }
        //设置空格输出开炮
        if(Input.GetKeyDown(KeyCode.Space))
        {
            Fire();
        }

    }
    private void Fire()
    {
        switch(nowType)
        {
            case E_FireType.One:
                Instantiate(bullet,this.transform.position,this.transform.rotation);
                break;
            case E_FireType.Two:
                Instantiate(bullet,this.transform.position-this.transform.right*0.5f,this.transform.rotation);
                Instantiate(bullet, this.transform.position + this.transform.right * 0.5f, this.transform.rotation);
                break;
           case E_FireType.Three:
                //面朝向
                Instantiate(bullet, this.transform.position, this.transform.rotation);
                Instantiate(bullet, this.transform.position, this.transform.rotation*Quaternion.AngleAxis(-20,Vector3.up));
                Instantiate(bullet, this.transform.position, this.transform.rotation * Quaternion.AngleAxis(20, Vector3.up));
                break;
            case E_FireType.Round:
                float angle = 360 / roundNum;
                for(int i=0; i<roundNum; i++)
                {
                    Instantiate(bullet, this.transform.position, this.transform.rotation * Quaternion.AngleAxis(i*angle, Vector3.up));
                }
                break;

        }
    }
}

子弹代码:

子弹是做成预制体储存起来的,以下是子弹逻辑代码:

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

public class Bullet : MonoBehaviour
{
    public float moveSpeed=20f;
    void Start()
    {
        //延迟5秒清空发射的子弹
        Destroy(this.gameObject,5);
    }

    // Update is called once per frame
    void Update()
    {
        this.transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
    }
}

要想做一个完整的飞机大战程序靠上面的代码是远远不够的,但是提供了最基本的逻辑代码,希望能够给你带来帮助。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Nicole Potter

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

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

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

打赏作者

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

抵扣说明:

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

余额充值