Unity3D简单小游戏脚本(1):EatCoin

今日闲来无事,敲些代码也无妨

 1 //coin脚本,命名为Pickup
 2 
 3 using System.Collections;
 4 using System.Colllections.Generic;
 5 using UnityEngine;
 6 
 7 public class Pickup : MonoBehaviour {
 8     //初始化  
 9     void start(){
10         }
11     //每帧刷新
12     void Updata(){
13         transform.Rotate(new Vector3(30,30,30)*Time.deltaTime); 
14         //随时间进行30°旋转
15         }
16 }
//player脚本,命名为Player

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

public class Player : MonoBehaviour {
    Rigidbody rigid;  //定义刚体
    float speed =3;  //设定速度
    int count = 0;    //设定金币数
    public AudioClip Pickup,Victory,Six,two,three,four,five; 
                                //定义各阶段的音效,并将其可见
    public UnityEngine.UI.Text tips;   //定义UI计分板
    // Use this for initialization
    void Start () {
        rigid = GetComponent<Rigidbody> (); //初始化刚体组件
    }
    
    // Update is called once per frame
    void Update () {
        var h = Input.GetAxis ("Horizontal");  //定义并得到输入的水平横轴位移
        var v = Input.GetAxis ("Vertical");   //定义并得到输入的水平纵轴位移


        var movement = new Vector3(h, 0, v);  //定义位移矢量

        rigid.AddForce (movement*speed); 
                                    //为刚体添加力,大小为位移矢量×速度
        Vector3 player =GetComponent<Transform>().position;
                                    //得到Player的当前三维坐标
        if (player.y < -3) {
            tips.text = "YOU LOST(失败没有音效,弟弟)";
                             //若纵轴小于-3,提示游戏失败
        }
    }
    private void OnTriggerEnter(Collider other)  //碰撞函数
    {
        if (other.GetComponent<Pickup> ()) { //如果发生碰撞
            other.gameObject.SetActive (false); //被碰撞者消失
            AudioSource.PlayClipAtPoint (Pickup, transform.position);
                                   //当前位置产生音效
            count++; //coin数增加

            RefreshTips (); //更新计分板
        }

    }
    void RefreshTips() //更新计分板函数
    {
        tips.text = " 你的金币数: " + count;  //显示文字
                //达到不同coin数时的提示音效
        if (count == 2) {
            AudioSource.PlayClipAtPoint (two, transform.position);
        }
        if (count == 3) {
            AudioSource.PlayClipAtPoint (three, transform.position);
        }
        if (count == 4) {
            AudioSource.PlayClipAtPoint (four, transform.position);
        }
        if (count == 5) {
            AudioSource.PlayClipAtPoint (five, transform.position);
        }
        if (count == 6) {
            AudioSource.PlayClipAtPoint (Six, transform.position);
            tips.text = " YOU WIN ";
                        //开始计时
            float m_time = 0f;
            m_time += Time.time;
            if (m_time >= 3f) {
                AudioSource.PlayClipAtPoint (Victory, transform.position); //延时播放,似乎没什么用QAQ
            }
        }
    
    }
}
    

游戏成品(渣的一批)

    链接:https://pan.baidu.com/s/187S1On-RZhvmhXHLWQ-NTQ 
    提取码:0uxq  
posted @ 2019-09-08 14:27  提莫队长正在送命  阅读(528)  评论(0编辑  收藏  举报