1、基本碰撞检测代码
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");
}
}
2、检测输入
function Update () {
if(Input.GetButtonUp("Jump")){
Debug.Log("We Have Hit the Space Bar!");
}
}
3、销毁对象
function Start () {
Destroy(gameObject.Find("Box"), 3);
}
4、实例来创建对象
//Simple Instantiation of a Prefab at Start
var thePrefab : GameObject;
function Start () {
var instance : GameObject = Instantiate(thePrefab, transform.position, transform.rotation);
}
把代码拖入到空GameJect上,然后把Prefab拖入到公共变量上。
5、简易定时器
var myTimer : float = 5.0;
function Update () {
if(myTimer > 0){
myTimer -= Time.deltaTime;
}
if(myTimer <= 0){
Debug.Log("GAME OVER");
}
}
6、物体在屏幕上移动
var speed : float = 5.0;
function Update () {
transform.Translate(Vector3(0,0,speed) * Time.deltaTime);
}
7、钢体向目标处移动
//Basic force to move a rigidbody object
var power : float = 500.0;
function Start () {
rigidbody.AddForce(Vector3(0,0,power));
}
8、碰撞然后转到下一场景
function OnCollisionEnter (Collision : Collision) {
if(gameObject.name == "Floor"){
Application.LoadLevel(myLevel);
}
}
floor---被动碰撞的的纲体
把代码拉到主动纲体上,然后场景设置:file----build seting----对话框,然后把当前场景拖里,然后把下一场景拖里,测试OK!
09 |
Debug.Log("Hit the wall"); |
10 |
|
11 |
} |
12 |
|
13 |
} |
Normal
0
7.8 磅
0
2
false
false
false
EN-US
ZH-CN
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin-top:0cm;
mso-para-margin-right:0cm;
mso-para-margin-bottom:10.0pt;
mso-para-margin-left:0cm;
line-height:11.0pt;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;}