Unity-3d 接小球游戏 各个脚本

移动Cube脚本

usingUnityEngine;

usingSystem.Collections;

publicclassPlayerMove:MonoBehaviour{

voidStart(){

}

voidUpdate(){

floathor=Input.GetAxis("Horizontal");

transform.position+=transform.right*Time.deltaTime*20*hor;

floatreal_x=Mathf.Clamp(transform.position.x,-4.05f,4.05f);

transform.position=newVector3(real_x,transform.position.y,transform.position.z);

}

voidOnTriggerEnter(Colliderother){

Destroy(other.gameObject);

GameInformation.GameInfo.myCoin++;

print("我的金币数量为:"+GameInformation.GameInfo.myCoin);

}

}

小球消失脚本

usingUnityEngine;

usingSystem.Collections;

publicclassSphereController:MonoBehaviour{

//Usethisforinitialization

voidStart(){

}

//Updateiscalledonceperframe

voidUpdate(){

if(transform.position.y<=0f){

Dead();

}

}

voidDead(){

Destroy(gameObject);

GameInformation.GameInfo.loseCoin++;

print("错过的金币数量为:"+GameInformation.GameInfo.loseCoin);

}

}

游戏控制脚本(预设体的生成及游戏结束的控制设置)

usingUnityEngine;

usingSystem.Collections;

publicclassGameController:MonoBehaviour{

publicGameObjectcoin;

floattime;

boolisGameOver;

voidStart(){

}

voidUpdate(){

if(!isGameOver){

time+=Time.deltaTime;

if(time>=2.0f){

floatrandom=Random.Range(-4.05f,4.05f);

Vector3v1=newVector3(random,10.0f,-0.5f);

Instantiate(coin,v1,Quaternion.identity);

time=0.0f;

}

if(GameInformation.GameInfo.loseCoin>=5){

GameOver();

}

}

}

voidGameOver(){

print("gameover");

isGameOver=true;

Time.timeScale=0;

}

}

全局控制脚本(不需要挂载对象,需要引用空间System.Collections.Generic)

usingUnityEngine;

usingSystem.Collections;

usingSystem.Collections.Generic;

publicclassGameInformation{

publicintmyCoin;

publicintloseCoin;

//单例

私有化构造方法

privateGameInformation(){

}

提供静态实例

private static GameInformation gameinfo;

提供获取静态实例的接口

public static GameInformation GameInfo {

get{

if(gameinfo==null){

gameinfo=newGameInformation();

GameInfo.myCoin=0;

GameInfo.loseCoin=0;

}

return   gameinfo;

}

}

}

你可能感兴趣的:(Unity-3d 接小球游戏 各个脚本)