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");


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM