unity2D打地鼠游戏

一、新建一个项目

打开unity软件——点击new——填写项目名称,改为2D模式,选择保存位置,点击创建项目。
在这里插入图片描述
在这里插入图片描述

二、导入资源、添加场景

  1. 新建一个文件夹,命名为sprites,把图片导入进去
  2. 新建一个audio文件夹,把声音导入进去
  3. 把ground图片和hole图片的Texture Type改为Sprite(2D and UI)后拉入场景中,如果场景中不显示hole的图片,证明hole的图片在ground图片的下一层,如果ground图片中 Sprite Render组件中Order in Layer为0,则hole图片为1。
    在这里插入图片描述
    在这里插入图片描述
    或者ground图片的Layer中添加一个Sorting Layers,为Ground,把Sprite Render组件中sorting Layer改为Ground,hole图片也是如此。
    在这里插入图片描述

三、调整Game窗口的大小

1.查看ground图片的大小
在这里插入图片描述
2.在Game窗口的Free Aspect 点击加号,命名为打地鼠,并且修改像素为ground图片的像素。
在这里插入图片描述

四、点击地鼠

1.把Gophers图片和Gophers_Beaten图片拉入场景中,添加一个Sorting Layer为mole,之后把Gophers_Beaten拉为预支体
2.给Gophers添加一个碰撞体Capsule Collider 2D,修改大小和Gophers一样大
3.新建一个Scripts文件夹,再新建一个script脚本,命名为s1,当鼠标点击就变为Gophers_Beaten。

	using System.Collections;
	using System.Collections.Generic;
	using UnityEngine;
		
	public class S1 : MonoBehaviour {
   		public GameObject mouseyun;
		// Use this for initialization
		void Start () {
		
		}
    	void OnMouseDown()
    		{
        Instantiate(mouseyun, transform.position, Quaternion.identity);
        Destroy(gameObject);
   		 }
   		 // Update is called once per frame
    		
}
  1. 把Gophers_Beaten拉到Mouseyun
    在这里插入图片描述

五、单个地鼠被击中后消失

1.把Gophers_Beaten拉入场景
2.在script文件夹中添加一c#脚本,命名为s2,目的是为了在出现0.5秒后消失

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

public class S2 : MonoBehaviour {

	// Use this for initialization
	void Start () {
        Destroy(gameObject, 0.5f);
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

六、随机位置生成老鼠

1.在场景中新建一个空物体
2.新建一个脚本为Shuiji,实现在9个位置随机1秒生成一个老鼠

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

public class ShuiJi : MonoBehaviour {
    public GameObject gameObject;
    //public Timer timer;
	// Use this for initialization
	void Start () {
        //Create();
        InvokeRepeating("Create", 0, 1);//1秒生成一个
        //timer.CountDown(true);
	}
	void Create()
    {
        Vector3 pos = Vector3.zero;
        int id = 0;
        id = Random.Range(1, 10);

        if (id == 1)       
        pos = new Vector3(-2, -1, 0);
        //Instantiate(gameObject, pos, Quaternion.identity);
        if (id == 2)
            pos = new Vector3(-2, 0, 0);
        //Instantiate(gameObject, pos, Quaternion.identity);
        if (id == 3)
            pos = new Vector3(-2, 1, 0);
        //Instantiate(gameObject, pos, Quaternion.identity);

        if (id == 4)
            pos = new Vector3(0, -1, 0);
        //Instantiate(gameObject, pos, Quaternion.identity);
        if (id == 5)
            pos = new Vector3(0, 0, 0);
        //Instantiate(gameObject, pos, Quaternion.identity);
        if (id == 6)
            pos = new Vector3(0, 1, 0);
        //Instantiate(gameObject, pos, Quaternion.identity);

        if (id == 7)
            pos = new Vector3(2, -1, 0);
        //Instantiate(gameObject, pos, Quaternion.identity);
        if (id == 8)
            pos = new Vector3(2, 0, 0);
        //Instantiate(gameObject, pos, Quaternion.identity);
        if (id == 9)
            pos = new Vector3(2, 1, 0);


        Instantiate(gameObject, pos, Quaternion.identity);
    }

}

3.把脚本拉到空物体上,并且把Gophers拉到GameObject上

在这里插入图片描述

七、得分

1.添加UI——Text,文字写为得分,设置大小,修改颜色
在这里插入图片描述
2.在S1脚本中添加代码,给Gophers一个被击中一次的得分
在这里插入图片描述
3.添加C#脚本Score,来实现分数的显示

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


public class Score : MonoBehaviour
{
    public static int score;
    Text text;
    // Use this for initialization
    void Start()
    {
        text = GetComponent<Text>();
        score = 0;
    }

    // Update is called once per frame
    void Update()
    {
        text.text = "得分:" + score;
    }
}

八、添加声音组件

1.找到预支体Gophers,添加Audio Sorce组件,把声音拉到audio clip上,Gophers_Beaten相同。
在这里插入图片描述

2.最后保存。完成此游戏。

问题及解决办法:

  1. 问题:在调整Game窗口的画布上出现问题,不能全部显示ground图片。
    解决办法:修改摄像机的大小:用像素高/2/100就为摄像机的大小,100为ground图片的分辨率。
  2. 问题:在做老鼠的随机生成的时候,老鼠与洞口不一致
    解决办法:修改每一个老鼠随机出现的位置,与自己游戏的位置相对应。

收获:

  1. 懂得了如何用摄像机大小来调整Game窗口的大小
  2. 复习了随机产生的运用和InvokeRepeating的使用
  3. 巩固了计分代码的使用。
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值