其他分享
首页 > 其他分享> > Unity3D常用代码集合

Unity3D常用代码集合

作者:互联网

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!
 Debug.Log("Hit the wall");

标签:function,Unity3D,Hit,Log,代码,myTimer,var,Debug,集合
来源: https://www.cnblogs.com/guangzhiruijie/p/15830275.html