unity8个入门代码
01,基本碰撞检测代码
function OnCollisionEnter(theCollision:Collision){
if(theCollision.gameObject.name=="Floor"){
Debug.Log("Hit the floor");
} else if(theCollision.gameObject.name=="Wall"){
Debug.Log("Hit the Wall);
}
}
02,检测输入
function Update(){
if( Input.GetButtonUp("Jump"){
Debug.Log("We Have HIt The Space Bar");
}
}
03,销毁对象
function Update(){
Destroy(game.Find("Box"),3);
}
04,实例创建对象
GameObject g=Instantiate(PREFAB,Pos,transform.rotation);
05,简易定时器
var myTimer: float =5.0f;
function Update(){
if(myTimer>0)
myTimer-=Time.deltaTime;}
if(myTimer<=0)
Debug.log("Game End");}
}
06,物体移动
var speed:float =5.0f;
function Update(){
transform.Translate(Vector3(0,0,speed)*Time.deltaTime);//Time.deltaTime:每秒耗时
}
07,刚体向目标处移动
var power:float =500.0f;
function Update(){
rigibody.AddForce(Vector3(0,0,power);//Time.deltaTime:每秒耗时
}
08,碰撞然后转到下一个场景
function OnCollisionEnter(theCollision:Collision){
if(theCollision.gameObject.name=="Floor"){
Application.LoadLevel("name");//参数亦可以是int型的
}
}