Unity小游戏2D(见缝插针)

本文介绍了如何使用Unity3D制作一款2D小游戏——见缝插针。步骤包括创建针和目标对象,设置对象属性,编写检测碰撞的代码,调整UI元素,以及实现游戏结束的逻辑。文章还分享了在游戏开发过程中遇到的问题和解决方案。

首先做一个针以及一个目标,可以自选比如箭矢射靶,网上自己找素材都行,注意目标最好是圆的。
图片拖入到unity中需要修改成下面的属性
在这里插入图片描述

创建一个目标命名为Circle、针命名为Pin、计算得分的Text、以及三个空物体命名如下:
在这里插入图片描述

InsPoint是针生成的地方,StartPoint是游戏开始的地方,两者之间距离远一些,InsPoint、StartPoint、Circle的x轴是一样的
在这里插入图片描述
1.修改text的属性
把宽和高修改为3,把字体大小修改为1,调整合适的大小以及调整到合适的位置。
在这里插入图片描述
在这里插入图片描述

2.在生成好的目标上挂上代码

public class RotateSelf : MonoBehaviour
{
    public float speed = 30;//小球旋转的速度
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.Rotate(new Vector3(0,0,-speed*Time .deltaTime));
        //这个是顺时针旋转 如果想要逆时针旋转把-speed前的-号删除即可
    }
}

这个是代表中间旋转的目标的速度

3.生成一个针,在针的物体上加入Rigidbody 2D把Gravity设置成0、Capsule Collider 2D调整到合适的大小并且修改Pin的Tag属性为PinHead。
然后在针上挂上代码

public class Pin : MonoBehaviour
{

    public bool isReah=false;
    public bool isFly=false;
    private Transform startPoint;
    private Transform circlePos;//圆的位置
    public float speed = 3;//针移动的速度
    private Vector3 endPoint;//结束位置
    // Start is called before the first frame update
    void Start()
    {
        startPoint = GameObject.Find("StartPoint").transform;
        circlePos=GameObject.Find("Circle").transform;
        endPoint = circlePos.position;
        endPoint.y -= 2.6f;
    }

    // Update is called once per frame
    void Update()
    {
        if (isFly == false)
        {
            if (isReah == false)
            {
                transform.position = Vector3.MoveTowards(transform.position, startPoint.position, speed * Time.deltaTime);
                if (Vector3.Distance(transform.position, startPoint.position) < 0.05f)
                {
                    isReah = true;
                   
                }
            }
        }
        else
        {
            transform.position = Vector3.MoveTowards(transform.position, endPoint, speed * Time.deltaTime);
            if (Vector3.Distance(transform.position, endPoint) < 0.05)
            {
                isFly = false;
                transform.parent = circlePos;
            }
        }
    }
    public void StartFly() {
        isFly = true;
        isReah = true;
    }
}

这个是检测到针碰到另一个针的时候结束游戏

public class PinHead : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "PinHead")
        {
            GameObject.Find("GameManger").GetComponent<GameManger>().GameOver();
            //游戏结束
        }
    }

}

然后把针移动到Perfab里生成一个预设体,并且把针的实体删除。

4.在GameManger挂上代码
在GameManger脚本中添加两个命名空间
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameManger : MonoBehaviour
{
    public GameObject PinPrefab;//针的预设体
    private Transform insPoint;//实例化的位置
    public Pin currentPin;//当前针
    public bool isOver;//判断当前是否结束
    public GameObject Circle;
    public Text scoreText;//分数显示的文本
    public int score = 0;//等分数
    public Camera mainCamera;
    public int speed=3;//渐变的速度
    // Start is called before the first frame update
    void Start()
    {
        insPoint = GameObject.Find("InsPoint").transform;
        InsPin();
        mainCamera = Camera.main;//获取主相机
    }

    // Update is called once per frame
    void Update()
    {
        if (isOver) return;
        //获取鼠标点击 0-鼠标左键 1-鼠标右键 2-鼠标中键
        if (Input.GetMouseButtonDown(0))
        {
            score++;
            scoreText.text = score.ToString();
            currentPin.StartFly();
            //Debug.Log("鼠标左键点击了一下");
            InsPin();
        }
    }
    //实例化针
    void InsPin() {
        currentPin = GameObject.Instantiate(PinPrefab, insPoint.position, PinPrefab.transform.rotation).GetComponent<Pin>();
    }

    public void GameOver() {
        if (isOver) return;
        Circle.GetComponent<RotateSelf>().enabled = false;
        isOver = true;
        StartCoroutine(GameOverAnimation());//开启协程
    }
    //协程
    IEnumerator GameOverAnimation() {
        while (true)
        {//切换游戏结束相机的颜色
            mainCamera.backgroundColor = Color.Lerp(mainCamera.backgroundColor, Color.red, speed * Time.deltaTime);
            mainCamera.orthographicSize = Mathf.Lerp(mainCamera.orthographicSize,4,speed*Time.deltaTime);
            if (Mathf.Abs(mainCamera.orthographicSize-4)<0.01)
            {
                break;
            }
            yield return 0;
        }
        //延迟调用
        yield return new WaitForSeconds(1);
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
}

按照这个把相应的东西挂载上
在这里插入图片描述

5.然后添加游戏结束的效果
修改相机的camera属性
在这里插入图片描述
这是修改完的样子。

因为这些也是我初次学习弄的,有什么问题请多多包涵~~~

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值