|
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 Start () {
Destroy(gameObject.Find("Box"), 3);
}
04、实例来创建对象
//Simple Instantiation of a Prefab at Start
var thePrefab : GameObject;
function Start () {
var instance : GameObject = Instantiate(thePrefab, transform.position, transform.rotation);
}
建立JAVA,把代码拖入到空GameJect上,然后把Prefab拖入到公共变量上,就可以了
05、简易定时器
var myTimer : float = 5.0;
function Update () {
if(myTimer > 0){
myTimer -= Time.deltaTime;
}
if(myTimer <= 0){
Debug.Log("GAME OVER");
}
}
06、物体在屏幕上移动
var speed : float = 5.0;
function Update () {
transform.Translate(Vector3(0,0,speed) * Time.deltaTime);
}
07、钢体向目标处移动
//Basic force to move a rigidbody object
var power : float = 500.0;
function Start () {
rigidbody.AddForce(Vector3(0,0,power));
}
08、碰撞然后转到下一场景
function OnCollisionEnter (myCollision : Collision) {
if(myCollision.gameObject.name == "Floor"){
Application.LoadLevel(myLevel);
}
}
floor---被动碰撞的的纲体
把代码拉到主动纲体上
然后
场景设置:file----build seting----对话框,然后把当前场景拖里,然后把下一场景拖里,测试OK |
|