9.8/9.11unity小项目总结

飞机大战

游戏场景

地图

选取了bg和bg1两张地图合在一起,实现无限循环的效果,第一张图bg移动到设定位置自动跳转到另一位置以此实现循环,附代码如下

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

public class ManagerMap : MonoBehaviour
{
    public Transform bg;//背景
    public Transform bg1;
    public float moveSpeed;//移动速度
    void Start()
    {
    
    }

    void Update()
    {
        //bg.Translate(0, -0.1f, 0);
        //Time.deltaTime:上一帧到当前帧用的时间;
        bg.Translate(0, moveSpeed*Time.deltaTime,0);
        bg1.Translate(0, moveSpeed * Time.deltaTime, 0);
        //Time.time该帧开始的时间(只读),此为自游戏启动以来的时间
        //Time.timeScale  时间流逝的缩放。可用于慢动作
        if (bg.position.z<=-8) 
        {
            bg.position = bg1.position + new Vector3(0, 0, 8);
        }
        if (bg1.position.z <=-8)
        {
            bg1.position = bg.position + new Vector3(0, 0, 8);
        }
    }
}

主角

实现主角可以wasd移动,发射子弹效果,子弹摧毁,死亡爆炸等效果

Input.GetKey()检测按键触发 transform.position获取设置物体的位置

transform.Translate坐标在水平和竖直方向上的移动距离

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

public class player : MonoBehaviour
{
    public float moveSpeed;//移动速度
    public GameObject bullet;//子弹
    public float attackInterval;//攻击间隔
    float tempTime;//记录累加时间
    public GameObject eff;
    #region 主角移动
    void PlayerMove()
    {
        if (Input.GetKey(KeyCode.W) && transform.position.z <= 3.5f)
        {
            transform.Translate(0, moveSpeed * Time.deltaTime, 0);
        }
        if (Input.GetKey(KeyCode.S) && transform.position.z >= -3.5f)
        {
            transform.Translate(0, -moveSpeed * Time.deltaTime, 0);
        }
        if (Input.GetKey(KeyCode.A) && transform.position.x >= -2.0f)
        {
            transform.Translate(-moveSpeed * Time.deltaTime, 0, 0);
        }
        if (Input.GetKey(KeyCode.D) && transform.position.x <= 2.0f)
        {
            transform.Translate(moveSpeed * Time.deltaTime, 0, 0);
        }
    }
    #endregion
    #region 主角攻击
    void PlayerAttack()
    {
        #region 发射子弹
        if (Input.GetKey(KeyCode.J))
        {
            tempTime += Time.deltaTime;
            if (tempTime >= attackInterval)
            {
                tempTime = 0;
                GameObject obj = Instantiate(bullet);//生成子弹
                obj.transform.position = transform.position;//指定子弹位置

            }
        }
        #endregion
        #region 发射炸弹
        if (Input.GetKey(KeyCode.K))
        {

        }
        #endregion
    }
    #endregion

    void Start()
    {
        tempTime = attackInterval;
    }

    // Update is called once per frame
    void Update()
    {
        PlayerMove();
        PlayerAttack();
    }
    private void OnTriggerEnter(Collider other)
    {

        if (other.tag == Tags.enemy)
        {
            Destroy(other.gameObject);//销毁子弹
            //生成爆炸物体
            GameObject obj = Instantiate(eff);//生成爆炸物体
            obj.transform.position = transform.position;//指定炸弹位置
            Destroy(obj, 1.5f);//销毁爆炸物体
            Destroy(transform.gameObject);//销毁当前物体

        }
    }
    private void OnDestroy()
    {
        UnityEngine.SceneManagement.SceneManager.LoadScene(0);
    }
}

敌人

实现和主角差不多的功能,后续附加刷新敌人,随机生成敌人。

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

public class Enemy : MonoBehaviour
{
    public float moveSpeed;
    public GameObject bullet;//子弹
    public float attackInterval;//攻击间隔
    float tempTime;//记录累加时间
    public GameObject eff;//爆炸物体
    public GameObject bulletBox;//子弹容器
    void Start()
    {
        bulletBox = GameObject.Find("ManagerMap");
    }
    void EnemyMove() 
    {
        transform.Translate(0, moveSpeed * Time.deltaTime, 0);
    }
    void EnemyAttack() 
    {
            tempTime += Time.deltaTime;
            if (tempTime >= attackInterval)
            {
                tempTime = 0;
                if (Random.Range(0, 10) > 3) //60%
                {
                    GameObject obj = Instantiate(bullet);//生成子弹
                    obj.transform.position = transform.position;//指定子弹位置
                    obj.transform.SetParent(bulletBox.transform);//指定子弹的父物
                }
            }      
    }
    void Update()
    {
        EnemyMove();
        EnemyAttack();
    }
    #region 触发器
    //private void OnCollisionEnter(Collision collision)
    //{
    //    Debug.Log("碰撞接触时执行" + collision.collider.name);
    //}
    //private void OnCollisionStay(Collision collision)
    //{
    //    Debug.Log("碰撞时执行" + collision.collider.name);
    //}
    //private void OnCollisionExit(Collision collision)
    //{
    //    Debug.Log("碰撞结束时执行" + collision.collider.name);
    //}
    #endregion

    private void OnTriggerEnter(Collider other)
    {      
         if(other.tag ==Tags.player)
        {
            Destroy(other.gameObject);//销毁子弹
            //生成爆炸物体
            GameObject obj = Instantiate(eff);//生成爆炸物体
            obj.transform.position = transform.position;//指定炸弹位置
            Destroy(obj,1.5f);//销毁爆炸物体
            Destroy(transform.gameObject);//销毁当前物体
            //生成道具
            }         
        }        
    }
    

下面代码实现敌人刷新,随机生成敌人,简单的处理胜利,由时间来计算胜利。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//敌人管理器

public class ManagerEnemy : MonoBehaviour

{
    public GameObject enemy;//敌人

    // Start is called before the first frame update
    void Start()
    { 
          //调用携程
        StartCoroutine(TimeInterval());//调用协程
        StartCoroutine("TimeInterval");//调用协程
        //StopCoroutine(TimeInterval());//终止协程(无用的)
        //StopCoroutine("TimeInterval");//终止协程
        //StopAllCoroutines();//终止所有协程
        //协程:在主程序运行的同时,开启另一段程序协助当前程序的运行
        //线程:
        //

    }

    // Update is called once per frame
    void Update()
    {
        
    }
    IEnumerator TimeInterval()
    {
        for(int boshu=0; boshu<10; boshu++)//波数
        {
           
            for(int diRen=0;diRen <10;diRen ++)//一波敌人
            {
                GameObject obj = Instantiate(enemy);//生成敌人
                float tempX = Random.Range(-1.8f, 1.8f);//随机x位置
                Vector3 v3 = new Vector3(tempX , 1, 5);
                obj.transform.position = v3 ;//指定敌人位置
                obj.transform.SetParent(transform);//指定父物体
                //obj.transform.SetParent(this.transform);
                yield return new WaitForSeconds(2f);//生成敌人时间间隔
            }
            yield return new WaitForSeconds(10);
            
        }
        //生成10波敌人每波间隔20s
        //每波生成10个敌人,每个敌人间隔1.5s
        yield return new WaitForSeconds(3);//等待三秒

        //通过时间来胜利
        UnityEngine.SceneManagement.SceneManager.LoadScene(0);
    }

}

子弹移动

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

public class BulletMove : MonoBehaviour
{
    public float moveSpeed;//子弹速度


    void Update()
    {
        transform.Translate(0, moveSpeed * Time.deltaTime,0 );
    }
}

边界

设置boundary边界来销毁超出边界的敌人和子弹

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

public class Boundary : MonoBehaviour
{
    private void OnTriggerExit(Collider other)
    {
        if (other.tag==Tags .player|| other.tag==Tags.enemy)
                {
            Destroy(other.gameObject);//删除物体
        }
    }
}

界面

菜单

设定简单的游戏界面,拥有开始游戏和结束游戏两个简单的功能

开始游戏和退出游戏功能的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class ManagerUI : MonoBehaviour
{
    public void BthStart()
    {
        SceneManager.LoadScene(1);//跳转场景
        //SceneManager.LoadScene("");//跳转场景
    }
    public  void BthExit()
    {
        Application.Quit();//退出项目
    }
}

游戏的发布

注意开始游戏场景是0,结束游戏场景是1,勾选后设置对应位置即可实现跳转功能,在所有完成之后就可以发布了

游戏的试玩

在所发布在位置找到可执行文件应用程序即可运行游戏,选择合适的分辨率即可游玩 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值