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、给小球添加游戏音效和吃掉金币的音效。

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

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

  • 9
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
Unity,为游戏添加金币音效是一种常见的增强用户体验的方法。当你设计一款游戏,当玩家角色(通常是游戏角色)成功收集到金币时,通常会播放一个特定的音效来表示这一事件的发生。以下是如何在Unity添加和使用金币音效的基本步骤: 1. **导入音效文件**: - 首先,你需要准备一个金币掉的音频文件,通常选择适合游戏风格的金币掉落或收集的音效(例如金币碰撞、金币拾取的wav或mp3格式)。 - 在Unity的Assets目录下创建一个专门的Audio Assets文件夹,将音效文件导入并存储其。 2. **创建播放器组件**: - 在Hierarchy面板,右键点击空处,选择Create > Audio > Audio Source,为游戏对象创建一个音频播放器组件。 3. **关联音效**: - 在创建的Audio Source组件上,找到"Clip"属性,在Inspector窗口设置为你要使用的金币音效。 4. **脚本控制**: - 如果你想在游戏基于代码触发音效,可以编写一个C#脚本,比如当检测到角色与金币碰撞时调用AudioSource.Play()方法播放音效。 ```csharp void OnTriggerEnter(Collider other) { if (other.CompareTag("Coin")) { AudioSource.PlayClipAtPoint(coinSound, transform.position); } } ``` 5. **设置循环**: - 如果金币掉后还想持续播放,你可以设置AudioSource.loop = true;。否则,用一次性的Play方法即可。 6. **优化性能**: - 为了减少内存消耗,考虑将音效设为一次性播放,或者使用Audio Clip实例池来复用同一音效对象。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

快乐的小码农turbo

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

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

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

打赏作者

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

抵扣说明:

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

余额充值