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

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 initialization
	void Start () {

        if(totalScore==null)
        {
            totalScore = FindObjectOfType(typeof(GUIShow)) as GUIShow;
            
        }
	}
	
	// Update is called once per frame
	void 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 initialization
	void Start () {
        
	}
	
	// Update is called once per frame
	void 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]);

               }
          
       }

   }
}



 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值