OT源代碼的分析,OrtHello 遲早攻破你 (十一)第6個例子,物理引擎和ortHello的結合(未完)

效果:

一開始

運行一段時間後




同樣是先貼代碼  以後再註釋



public class CExample6 : MonoBehaviour {

    // rotate a physical static object
    void Rotate(string name)
    {
        OTObject o = OT.ObjectByName(name);
        if (o != null)
            o.rotation += (90 * Time.deltaTime);
    }

    // destroy a 'falling' object/sprite as soon as it is out of view
    void DestroyWhenOutOfView(OTObject owner)
    {
        OT.DestroyObject(owner);
    }
    
    float it = 0;   
    // Update is called once per frame
    void Update () {
        it += Time.deltaTime;
        if (it > 0.15f)
        {
            // check each 0.15 seconds if we want to create a 'falling' sprite
            it = 0;
            if (Random.value > 0.65f)
            {
                // create a 'falling' sprite
                OTSprite sp = null;
                float si = 20 + Random.value * 50;
                if (Random.value > 0.5f)
                {
                    // lets create a new block from prototype
                    sp = OT.CreateObject("block").GetComponent<OTSprite>();
                    sp.size = new Vector2(si, 20 + Random.value * 50);
                }
                else
                {
                    // lets create a new star from prototype
                    sp = OT.CreateObject("star").GetComponent<OTSprite>();
                    sp.size = new Vector2(si, si);
                }

                sp.gameObject.rigidbody.mass = si;
                sp.position = new Vector2(-200 + Random.value * 400, 300);
                sp.onOutOfView = DestroyWhenOutOfView;
            }
        }

        Rotate("rot");
        Rotate("rot_s1");
        Rotate("rot_s2");
        Rotate("rot_s3");
    }
}
發佈了32 篇原創文章 · 獲贊 3 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章