Unity 无重力系统打飞碟

首先设计关卡:

Round 1:白色飞碟(大,慢)
Round 2 : 白色飞碟 + 蓝色飞碟(小,慢)
Round 3: 红色飞碟 (小,快)

实验要求是所造的飞碟数不能超过N个,也就是循环利用,这里我令N等于6

游戏截图如下:

Unity 无重力系统打飞碟_第1张图片

点击飞碟则为打中

Unity 无重力系统打飞碟_第2张图片

一些知识点

  1. 简单工厂模式创建对象
  2. 两种延迟执行函数的方法
  3. 对于多Round的变化,应该怎么处理

具体代码如下,代码中有注释,把两份代码挂载在Main Camera上就可以运行了

Hitfeidie.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Com.myhitgame;

namespace Com.myhitgame {
    public class myFactory : System.Object {
        //values
        public int Round = 0; // now round = ?
        public int Isbegin = 0; // 0 == not begin
        public int Score = 0;
        public int usefri = 0;

        //firsbees
        public List<GameObject> fris = new List<GameObject>();


        private static myFactory _instance;
        public static myFactory GetInstance(){
            if (_instance == null) {
                _instance = new myFactory ();
            }
            return _instance;
        }

        public void launchAfrisbee(float g,float delaytime,Vector3 speed,Color color,
            Vector3 size,Vector3 position){
            if (fris.Count == 0) {
                GameObject fri = GameObject.CreatePrimitive (PrimitiveType.Cylinder);
                waittolanch wt = fri.AddComponent<waittolanch> ();
                wt.setting (g,fri,fris,delaytime,speed,color,size,position);
            } else {
                GameObject fri = fris [0];
                fris.RemoveAt (0);
                //这里非常重要,用了get而不是add
                waittolanch wt = fri.GetComponent<waittolanch> ();
                wt.setting (g,fri,fris,delaytime,speed,color,size,position);
            }
        }

        public class waittolanch : MonoBehaviour {
            public List<GameObject> fris;
            public GameObject fri;
            public Camera cm;
            public float delaytime;
            public Vector3 speed;
            public float g;
            public Color color;
            public Vector3 size;
            public Vector3 position;
            public int state = 0;


            public void setting(float g,GameObject fri,List<GameObject> fris,float delaytime,
                Vector3 speed,Color color,Vector3 size,Vector3 position) {
                this.fri = fri;
                this.fris = fris;
                this.delaytime = delaytime;
                this.speed = speed;
                this.g = g;
                this.color = color;
                this.size = size;
                this.position = position;
                StartCoroutine (launch());
            }

            public IEnumerator launch(){
                //wait delaytime
                yield return new WaitForSeconds (delaytime);
                //lauch a fri
                this.transform.position = position;
                this.transform.localScale = size;
                this.GetComponent<Renderer> ().material.color = color;
                state = 1;
            }

            void Update(){
                if (state == 1) {
                    speed = new Vector3 (speed.x, speed.y - g, speed.z);
                    this.transform.position = this.transform.position + speed;
                    if (this.transform.position.y <= 0f) {
                        print ("落地了");
                        this.transform.position = new Vector3 (0f,0f,-2f);
                        state = 0;
                        // when download
                        fris.Add (fri);
                        myFactory.GetInstance ().usefri++;

                    }
                }
                if(Input.GetMouseButtonDown(0)){
                    Vector3 mp = Input.mousePosition;
                    cm = Camera.main;
                    Ray ray = cm.ScreenPointToRay (mp);
                    RaycastHit hit;
                    if(Physics.Raycast(ray,out hit)){
                        if(hit.collider.gameObject == this.gameObject){
                            print ("jizhong");
                            this.transform.position = new Vector3 (0f,0f,-2f);
                            state = 0;
                            // when download
                            fris.Add (fri);
                            myFactory.GetInstance ().Score++;
                            myFactory.GetInstance ().usefri++;
                        }
                    }
                }
            }

        }
    }
}

public class Hitfeidie : MonoBehaviour {
    public Vector3 camerapos;
    public Quaternion cameraqua;
    public Camera cm;
    // color
    public Color whitefricolor;
    public Color bluefircolor;
    public Color Redfircolor;
    //Size
    public Vector3 whitefriSize;
    public Vector3 bluefriSize;
    public Vector3 redfriSize;
    //speed
    public Vector3 whitefrispeed;
    public Vector3 bluefrispeed;
    public Vector3 redfrispeed;
    //a little distance use to make some shift
    public Vector3 littledistance;
    //a g use to change v
    public float g;
    //position
    public Vector3 liftposition;
    public Vector3 rightposition;
    // Use this for initialization
    void Start () {
        //make a plane and make camra in the right position
        camerapos = new Vector3(0f,0f,0f);
        cameraqua = Quaternion.Euler(340f,0f,0f);
        cm = Camera.main;
        cm.transform.position = camerapos;
        cm.transform.rotation = cameraqua;
        //初始化

        // color
        whitefricolor = new Color(0.9f,0.9f,0.9f);
        bluefircolor = new Color (0f, 0f, 0.7f);
        Redfircolor = new Color (0.7f,0f,0f);
        //Size
        whitefriSize = new Vector3(1.2f,0.5f,1.2f);
        bluefriSize = new Vector3 (1f,0.2f,1f);
        redfriSize = new Vector3 (1f,0.2f,1f);
        //speed
        whitefrispeed = new Vector3(0f,0.1f,0.25f);
        bluefrispeed = new Vector3 (0f,0.1f,0.25f);
        redfrispeed = new Vector3(0f,0.1f,0.4f);
        //a little distance use to make some shift
        littledistance = new Vector3(0.05f,0f,0f);
        //a g use to change v
        g = 0.001f;
        //position
        liftposition = new Vector3(-1f,0f,0f);
        rightposition = new Vector3 (1f,0f,0f);

    }

    // Update is called once per frame
    void Update () {
        if (myFactory.GetInstance ().Round == 1 && myFactory.GetInstance ().Isbegin == 0) {
            //Isbegin = 1;
            myFactory.GetInstance().Isbegin = 1;
            // function round1
            Invoke ("Round1",0f);
        }

        if(myFactory.GetInstance ().Round == 1 && myFactory.GetInstance ().usefri == 6){
            myFactory.GetInstance ().Round = 2;
            myFactory.GetInstance ().usefri = 0;
            Invoke ("Round2",1f);
        }

        if(myFactory.GetInstance ().Round == 2 && myFactory.GetInstance ().usefri == 6){
            myFactory.GetInstance ().Round = 3;
            myFactory.GetInstance ().usefri = 0;
            Invoke ("Round3",1f);
        }
    }

    void Round1(){
        for (int i = 1;i<=6; i++){
            float time = i;
            //random
            float Randkey = Random.Range (-4f,4f);
            if(i%2 == 0) {
                myFactory.GetInstance ().launchAfrisbee (g,time*2,whitefrispeed+littledistance*Randkey,whitefricolor,whitefriSize,liftposition);
            } else {
                myFactory.GetInstance ().launchAfrisbee (g,time*2,whitefrispeed-littledistance*Randkey,whitefricolor,whitefriSize,rightposition);
            }
        }
    }

    void Round2(){
        for (int i = 1;i<=6; i++){
            float time = i;
            float Randkey = Random.Range (-4f,4f);
            if(i%2 == 0) {
                myFactory.GetInstance ().launchAfrisbee (g,time*2,whitefrispeed+littledistance*Randkey,whitefricolor,whitefriSize,liftposition);
            } else {
                myFactory.GetInstance ().launchAfrisbee (g,time*2,bluefrispeed-littledistance*Randkey,bluefircolor,bluefriSize,rightposition);
            }
        }
    }

    void Round3(){
        for (int i = 1;i<=6; i++){
            float time = i;
            float Randkey = Random.Range (-4f,4f);
            if(i%2 == 0) {
                myFactory.GetInstance ().launchAfrisbee (g,time*2,redfrispeed+littledistance*Randkey,Redfircolor,redfriSize,liftposition);
            } else {
                myFactory.GetInstance ().launchAfrisbee (g,time*2,redfrispeed+littledistance*Randkey,Redfircolor,redfriSize,rightposition);
            }
        }
    }

}
OnGUi.cs
using UnityEngine;
using System.Collections;
using Com.myhitgame;

public class OnGUi : MonoBehaviour {

    float beginButtonX = Screen.width/2-Screen.width/8;
    float beginButtonY = Screen.height/2-Screen.width/16;
    float beginButtonW = Screen.width/4;
    float beginButtonH = Screen.height/8;
    int clicked = 0;
    // Use this for initialization
    void Start () {

    }

    void OnGUI(){
        //how to make a clickanddispear button
        if (clicked == 0) {
            if (GUI.Button (new Rect (beginButtonX, beginButtonY, beginButtonW, beginButtonH), "开始游戏")) {
                myFactory.GetInstance ().Round = 1;
                clicked = 1;
            }
        }
        GUIStyle s = new GUIStyle ();
        s.fontSize = 40;
        s.normal.textColor = new Color (0.7f,0.7f,0.7f);
        s.normal.background = null;
        // Round
        string round = "Round: "+myFactory.GetInstance().Round.ToString();
        GUI.Label (new Rect(beginButtonW/4,0,beginButtonW,beginButtonH),round,s);
        //Score
        string score = "Score: "+myFactory.GetInstance().Score.ToString();
        GUI.Label (new Rect(beginButtonW/4,beginButtonH,beginButtonW,beginButtonH),score,s);
    }

}

你可能感兴趣的:(unity)