unity3D 接雞蛋小遊戲
阿新 • • 發佈:2019-02-16
public class EggScore : MonoBehaviour {
public int score;
// Use this for initialization
void Start () {
score=0;
}
// Update is called once per frame
void Update () {
rigidbody.velocity=new Vector3(Input.GetAxis("Horizontal")*10,0,0);//使箱子沿x軸移動
if(Vector3.Distance(rigidbody.position,new Vector3(6.1f,0.52f,-2f))<0.1f && rigidbody.velocity==Vector3.right*10)//使箱子超過右側邊界時從邊界左側出來
{
rigidbody.position=new Vector3(-6.1f,0.52f,-2f);
}
if(Vector3.Distance(rigidbody.position,new Vector3(-6.1f,0.52f,-2f))<0.1f&& rigidbody.velocity==-Vector3.right*10)//使箱子超過左側邊界時從邊界右側出來
{
rigidbody.position=new Vector3(6.1f,0.52f,-2f);
}
}
void OnTriggerEnter(Collider col)//觸發器(新增到箱子中)來計算分數
{
if(col.gameObject.name=="Egg(Clone)")
{
score++;
print(score);
}
//print (col.gameObject.name);
}
void OnGUI()//在介面顯示接到的蛋數
{
GUI.Button(new Rect(0,0,30,30),score.ToString());
}
public int score;
// Use this for initialization
void Start () {
score=0;
}
// Update is called once per frame
void Update () {
rigidbody.velocity=new Vector3(Input.GetAxis("Horizontal")*10,0,0);//使箱子沿x軸移動
if(Vector3.Distance(rigidbody.position,new Vector3(6.1f,0.52f,-2f))<0.1f && rigidbody.velocity==Vector3.right*10)//使箱子超過右側邊界時從邊界左側出來
{
rigidbody.position=new Vector3(-6.1f,0.52f,-2f);
}
if(Vector3.Distance(rigidbody.position,new Vector3(-6.1f,0.52f,-2f))<0.1f&& rigidbody.velocity==-Vector3.right*10)//使箱子超過左側邊界時從邊界右側出來
{
rigidbody.position=new Vector3(6.1f,0.52f,-2f);
}
}
void OnTriggerEnter(Collider col)//觸發器(新增到箱子中)來計算分數
{
if(col.gameObject.name=="Egg(Clone)")
{
score++;
print(score);
}
//print (col.gameObject.name);
}
void OnGUI()//在介面顯示接到的蛋數
{
GUI.Button(new Rect(0,0,30,30),score.ToString());
}
}