unity体感游戏--接钻石游戏(三)游戏物体碰撞得分

来源:互联网 发布:苹果6s怎么切换3g网络 编辑:程序博客网 时间:2024/04/27 22:39

u3d的碰撞函数是OnTriggerEnter()代码如下

using UnityEngine;using System.Collections;public class onCollider : MonoBehaviour {    public GameObject object1;    public GameObject object2;    public GameObject object3;    private GUIShow totalScore;//总得分// Use this for initializationvoid Start () {        if(totalScore==null)        {            totalScore = FindObjectOfType(typeof(GUIShow)) as GUIShow;                    }}// Update is called once per framevoid Update () {       }    void OnTriggerEnter(Collider other)     {        print(other.collider.gameObject.name);        if (other.collider.gameObject.name.Contains(object1.gameObject.name))        {            Destroy(other.collider.gameObject);            totalScore.TotalScore1();            gameObject.audio.Play();//添加声音播放                    }        else if (other.collider.gameObject.name.Contains(object2.gameObject.name))        {                       Destroy(other.collider.gameObject);            totalScore.TotalScore2();            gameObject.audio.Play();                  }        else if (other.collider.gameObject.name.Contains(object3.gameObject.name))        {                       Destroy(other.collider.gameObject);            totalScore.TotalScore3();            gameObject.audio.Play();                   }        else         {           return;        }    }        }

碰撞之后得分代码

using UnityEngine;using System.Collections;public class GUIShow : MonoBehaviour {    private int scoreTpye1 = 0;    private int scoreTpye2 = 0;    private int scoreTpye3 = 0;    public int totalScore;    public string myStringScore;        public float x = 85;    public float y = 19;    public float scale = 1;    public Color myColor;    //定义数组    public Texture[] myNumber = new Texture[10];    //public Texture Tex;    //    private int index = 0;    private int width = 30;    private int height = 72;    //private displayScore displayTotalScore;// Use this for initializationvoid Start () {        }// Update is called once per framevoid Update () {        totalScore = scoreTpye3 + scoreTpye1 + scoreTpye2;        //print(totalScore);        myStringScore = totalScore.ToString();      //  print("GUIShowA" + totalScore);}    public void TotalScore1()     {        scoreTpye1 += 10;      //  print(scoreTpye1);    }    public void TotalScore2()     {        scoreTpye2 += 20;        //print(scoreTpye2);    }    public void TotalScore3()     {        scoreTpye2 += 30;        //print(scoreTpye3);    }   void OnGUI()   {       GUI.color = myColor;       if (myStringScore != null)       {                         for (int i = 0; i < myStringScore.Length; i++)               {                   GUI.DrawTexture(new Rect(x + i * scale * width, y, scale * width, scale * height),                   myNumber[int.Parse(myStringScore.Substring(i, 1))], ScaleMode.StretchToFill, true, 0);                   //GUI.DrawTexture(new Rect(x + i * scale * width, y, scale * width, scale * height),myNumber[myStringScore[i]-48]);               }                 }   }}



 

 

原创粉丝点击