2011年11月28日 星期一

【進階教學】齒輪碰撞 - Unity Gear Collision

1.建立Maya齒輪
2.匯入FBX至Unity

3.製作旋轉齒輪
4.建立齒輪碰撞基體 方塊和圓餅
5.將齒輪的Mesh與方塊碰撞基體放入圓餅碰撞基體中
6.關閉圓餅碰撞基體

7.製作被動齒輪
8.將製作好的旋轉齒輪複製一個出來
9.建立空的GameObject
10.將所有旋轉齒輪的物件拉入GameObject
11.新增RigiBody並關閉use Gravity

12.將兩個齒輪擺在適當位置
13.旋轉齒輪加入AutoRotate.js




var gear:GameObject;
var downSpeed:float;
var rotateSpeed:float;
var stop:boolean;
var isRotate:boolean;
var isR:boolean;
var flag:boolean;
var flagB:boolean;
var start:int=0;

function Start(){
    x=-0.5f;
    this.name="Gear"+gobal.nomber.ToString();
    gobal.nomber++;
    stop=false;
    flag=true;
    flagB=true;
}

function FixedUpdate () {
    if(flagB==true){
        flag=transform.gameObject.GetComponent(SnycRotate).flag;
        flagB=flag;
    }
    
    if(flag==false){
        isRotate=(transform.gameObject.GetComponent(SnycRotate).isRotate);
        isR=(transform.gameObject.GetComponent(SnycRotate).isR);
        flag=true;
        Debug.Log("Down flag="+flag);
        Debug.Log("Down isR="+isR);
    }
    
    //Debug.Log("isRotate="+isRotate);
    //Debug.Log("isR="+isR);
    
    if(stop==false){
        downSpeed+=(0.5f*Time.deltaTime);
        if(downSpeed>=0.5f){
            transform.rigidbody.position += Vector3(0,-10,0);
            downSpeed=0;
            
        }
        
        if(Input.GetKeyDown("left"))
            this.transform.rigidbody.position += Vector3(-10,0,0);
        
        
        if(Input.GetKeyDown("right"))
            this.transform.rigidbody.position += Vector3(10,0,0);
        
        
        if(Input.GetKeyDown("down"))
            this.transform.rigidbody.position += Vector3(0,-10,0);
        
        
        if(Input.GetKeyDown("space")){
            start=1;
            stop=true;
            Instantiate(gear, Vector3(-32,100,-19),Quaternion.identity);
        }
        
        
    }
    
    if(isRotate==true){
                rotateSpeed+=(0.25f*Time.deltaTime);
                if(rotateSpeed>=0.25f){
                    if(isR==true){
                        this.transform.Rotate(Vector3(0,0,-45*start));
                        rotateSpeed=0;
                    }else{
                        this.transform.Rotate(Vector3(0,0,45*start));
                        rotateSpeed=0;
                    }
                }
                
            }
    
}

0 ♥:

張貼留言