unity体感ゲーム--ダイヤモンドゲーム(三)ゲーム物体衝突得点
3255 ワード
u 3 dの衝突関数は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]);
}
}
}
}