概述
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]);
}
}
}
}
最后
以上就是冷艳母鸡为你收集整理的unity体感游戏--接钻石游戏(三)游戏物体碰撞得分的全部内容,希望文章能够帮你解决unity体感游戏--接钻石游戏(三)游戏物体碰撞得分所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复