Unity迷宫中吃金币

1、新建3D项目-->命名并创建项目

 2、右键点击3D Object新建平面Plane,调整平面大小,并且平面不要设置太大,否则后期小球会穿墙,在Assert中选择一个迷宫的贴图并将它给平面。

3、创建Cube,将它设置的和下边的迷宫图重合,还可以给Cube添加材质Material,换上自己喜欢的颜色

 4、添加Sphere,修改名字为player,调整小球的位置,并设置材质。

 5、添加move脚本,将脚本拖给小球,使小球能够通过WASD或上下左右移动。

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


public class move : MonoBehaviour
{
	public float speed=1;
    // Start is called before the first frame update
    void Start()
    {   

    }
  // Update is called once per frame
    void Update()
    {
        float v = Input.GetAxis("Vertical"); //按‘W’'S'键返回垂直方向[-1,1]的值
        float h = Input.GetAxis("Horizontal");//按‘A’'D'键返回水平方向[-1,1]的值      
        transform.Translate(h * speed * Time.deltaTime, 0, v * speed * Time.deltaTime);
      
    } 
}

6、选中小球,选择右侧的Add Component,搜索Rigidbody,选择然后选中Use Gravity,点开Constrains,将这些打勾,否则小球会乱转。

7、添加金币,将其放在迷宫的各个位置,新建一个create empty,改名为coin,将所有小球放在coin里,添加rorate脚本,选中一个小球,点shift,点击最后一个小球,就可选中所有的小球,将脚本给所有小球,让所有小球旋转。

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

public class rorate : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        this.transform.Rotate(Vector3.up,Space.World);
    }
}

8、给move脚本添加内容,使小球碰到金币就会吃掉它。给所有小球添加tag ,命名为coin,并选择,然后添加is Trigger,这样就实现小球移动,金币旋转,和小球吃掉金币的效果。

 private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "coin")
        {
            Destroy(other.gameObject);
                 
        }
    }

9、新建一个Panel,实现计时和得分的效果,添加text文本,修改文本为score和time,再设置win和fail的文本。

 10、继续在move里面添加代码,当吃掉小球时,进行计分。并新建一个空项目,设置倒计时,新建倒计时TimeDown的脚本将其给新建的空项目。

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


public class move : MonoBehaviour
{
	public float speed=1;
	private int score_num = 0;
	public Text score;
	public GameObject win;
  
    // Start is called before the first frame update
    void Start()
    {
        win.SetActive(false);

       

    }

    // Update is called once per frame
    void Update()
    {
        float v = Input.GetAxis("Vertical"); //按‘W’'S'键返回垂直方向[-1,1]的值
        float h = Input.GetAxis("Horizontal");//按‘A’'D'键返回水平方向[-1,1]的值      
        transform.Translate(h * speed * Time.deltaTime, 0, v * speed * Time.deltaTime);
        if (Input.GetKeyDown(KeyCode.R))
        {
            SceneManager.LoadScene(0);
            Time.timeScale = 1;
            return;
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "coin")
        {

        	score_num++;
            score.text = "score: " + score_num.ToString();//显示字符串,字符串控制符
          
            Destroy(other.gameObject);
            
               if (score_num == 10)
            {
            	win.SetActive(true);
                Time.timeScale = 0;//运行时间倍率,0代表游戏停滞
               
            }

        }
    }

}

 倒计时脚本:

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

public class TimeDown : MonoBehaviour
{

	private float CountDownTime = 29f;
	private float GameTime;
	private float timer = 0;
	public Text GameCountTimeText;
	public GameObject over;
	private AudioSource au;

	private void Start()
	{
		GameTime = CountDownTime;
		over.SetActive(false);
		au = GetComponent<AudioSource>();
	}
	private void Update()
	{

		int M = (int)(GameTime / 60);
		float S = GameTime % 60;
		timer += Time.deltaTime;

		if (timer >= 1f)
			{
				timer = 0;
				GameTime--;
				GameCountTimeText.text = "time"+M + ":" + string.Format("{0:00}", S);
				if (S <= 0)
				{
					//au.Play();
					over.SetActive(true);
				//结束游戏操作
					Time.timeScale = 0;
					
				}
			}

		

	}
	
}

11、给小球添加游戏音效和吃掉金币的音效。

保存并运行,便可以运行游戏了。

还可以添加游戏开始和游戏结束的页面。

  • 8
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
很高兴为您解答关于Unity开发小球迷宫金币游戏的问题。 下面是实现的步骤: 1. 创建地图。在Unity创建一个平面,然后使用建模软件或Unity的Terrain工具将其变形成迷宫地图。 2. 创建小球。可以使用Unity的内置形状或导入自己的模型。将小球添加到场景,并将其放在地图的起点。 3. 创建金币。可以使用Unity的内置形状或导入自己的模型。将金币添加到场景,并将其放在地图的某些位置。 4. 使用脚本控制小球移动。创建一个脚本,将其附加到小球上,并编写代码以响应玩家的输入控制小球的移动。例如: ``` public float speed = 5.0f; void Update() { float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); transform.Translate(new Vector3(h, 0, v) * speed * Time.deltaTime); } ``` 这个脚本将根据玩家的输入控制小球的移动。 5. 创建触发器。在金币对象上添加一个触发器组件(Collider),当小球接触到金币时,触发器将触发事件。 6. 编写触发事件的代码。例如: ``` void OnTriggerEnter(Collider other) { if(other.gameObject.CompareTag("Coin")) { GameManager.instance.AddScore(); Destroy(other.gameObject); } } ``` 这个代码段的作用是当小球接触到金币时,GameManager实例的AddScore方法将增加分数,并销毁金币对象。 7. 在GameManager编写AddScore方法: ``` public void AddScore() { totalScore++; scoreText.text = "Score: " + totalScore.ToString(); } ``` 这个方法的作用是将分数加1,并更新游戏界面上的分数显示。 以上就是一个简单的Unity小球迷宫金币游戏的开发流程,希望能对您有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

快乐的小码农turbo

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

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

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

打赏作者

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

抵扣说明:

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

余额充值