unity3d 飞碟游戏
生活随笔
收集整理的這篇文章主要介紹了
unity3d 飞碟游戏
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
編寫一個簡單的鼠標打飛碟(Hit UFO)游戲
動作管理器統(tǒng)一接口
public interface SceneController {void load();void game(int Round);int Choose(float[] probe); }飛碟隨機運動
using UnityEngine; using System.Collections;public class move1 : MonoBehaviour {public float speedX = 5.0F;public float speedY = 5.0F;public float speedZ = 5.0F;// Use this for initializationfloat rand1 = Random.Range(-1.0f, 1.0f);float rand2 = Random.Range(-1.0f, 1.0f);float rand3 = Random.Range(-1.0f, 1.0f);float randx;float randy;void Start () {randx = Random.Range(-1.0f, 1.0f);randy = Random.Range(-1.0f, 1.0f);//Debug.Log(randx);//Debug.Log(randy);if (randx>0)randx = 1.0f;elserandx = -1.0f;if (randy >0)randy = 1.0f;elserandy = -1.0f;}// Update is called once per framevoid Update () {float translationY = 1 * speedY*rand1*randx;float translationX = 1 * speedX*rand2*randy;float translationZ = 1 * speedZ;translationY *= Time.deltaTime;translationX *= Time.deltaTime;//transform.Translate(0, translationY, 0);//transform.Translate(translationX, 0, 0);transform.Translate(translationX, translationY, translationZ);}}工廠
using UnityEngine;namespace SimpleFactory {#region 產(chǎn)品public interface UFO{void Version(int round);}public class UFO1 : UFO{float x = Random.Range(-5.0f, 5.0f);public virtual void Version(int round){GameObject UFO1;Vector3 UFO1Pos = new Vector3(x, 0, 0);UFO1 = Object.Instantiate(Resources.Load("UFO1", typeof(GameObject)), UFO1Pos, Quaternion.identity) as GameObject;UFO1.name = "UFO1";UFO1.AddComponent<PickupMultiObjects>();move1 test = UFO1.AddComponent<move1>();test.speedX *= (float)round;test.speedY *= (float)round;test.speedZ = 0.0f ;}}public class UFO2 : UFO{float x = Random.Range(-5.0f, 5.0f);public virtual void Version(int round){GameObject UFO1;Vector3 UFO1Pos = new Vector3(x, 0, 0);UFO1 = Object.Instantiate(Resources.Load("UFO2", typeof(GameObject)), UFO1Pos, Quaternion.identity) as GameObject;UFO1.name = "UFO2";UFO1.AddComponent<PickupMultiObjects>();move1 test = UFO1.AddComponent<move1>();test.speedX *= (float)round;test.speedY *= (float)round;test.speedZ = 0.0f;Debug.Log("2222");}}public class UFO3 : UFO{float x = Random.Range(-5.0f, 5.0f);public virtual void Version(int round){GameObject UFO1;Vector3 UFO1Pos = new Vector3(x, 0, 0);UFO1 = Object.Instantiate(Resources.Load("UFO3", typeof(GameObject)), UFO1Pos, Quaternion.identity) as GameObject;UFO1.name = "UFO3";UFO1.AddComponent<PickupMultiObjects>();move1 test = UFO1.AddComponent<move1>();test.speedX *= (float)round;test.speedY *= (float)round;test.speedZ = 0.0f;Debug.Log("333");}}public class UFO4 : UFO{float x = Random.Range(-5.0f, 5.0f);public virtual void Version(int round){GameObject UFO1;Vector3 UFO1Pos = new Vector3(x, 0, 0);UFO1 = Object.Instantiate(Resources.Load("UFO4", typeof(GameObject)), UFO1Pos, Quaternion.identity) as GameObject;UFO1.name = "UFO4";UFO1.AddComponent<PickupMultiObjects>();move1 test = UFO1.AddComponent<move1>();test.speedX *= (float)round;test.speedY *= (float)round;test.speedZ = 0.0f;Debug.Log("4444");}}public class UFO5 : UFO{float x = Random.Range(-5.0f, 5.0f);public virtual void Version(int round){GameObject UFO1;Vector3 UFO1Pos = new Vector3(x, 0, 0);UFO1 = Object.Instantiate(Resources.Load("UFO5", typeof(GameObject)), UFO1Pos, Quaternion.identity) as GameObject;UFO1.name = "UFO5";UFO1.AddComponent<PickupMultiObjects>();move1 test = UFO1.AddComponent<move1>();test.speedX *= (float)round;test.speedY *= (float)round;test.speedZ = 0.0f;Debug.Log("5555");}}#endregion#region 工廠public interface IFactory{UFO Create();}public class UFO1Factory : IFactory{public virtual UFO Create(){return new UFO1();}}public class UFO2Factory : IFactory{public virtual UFO Create(){return new UFO2();}}public class UFO3Factory : IFactory{public virtual UFO Create(){return new UFO3();}}public class UFO4Factory : IFactory{public virtual UFO Create(){return new UFO4();}}public class UFO5Factory : IFactory{public virtual UFO Create(){return new UFO5();}}#endregion }場景單實例
using UnityEngine; using System.Collections;public class Singleton<T> : MonoBehaviour where T : MonoBehaviour {protected static T instance;public static T Instance{get{if (instance == null){instance = (T)FindObjectOfType(typeof(T));if (instance == null){Debug.LogError("An instance of " + typeof(T) +" is needed in the scene, but there is none.");}}return instance;}} }場景單實例的使用很簡單,你僅需要將 MonoBehaviour 子類對象掛載任何一個游戲?qū)ο笊霞纯伞?/p>
然后在任意位置使用代碼 Singleton <>.Instance 獲得該對象。
光標拾取多個物體程序
public class PickupMultiObjects : MonoBehaviour {public GameObject cam;// Update is called once per framevoid Update () {if (Input.GetButtonDown("Fire1")) {Debug.Log ("Fired Pressed");Debug.Log (Input.mousePosition);Vector3 mp = Input.mousePosition; //get Screen Position//create ray, origin is camera, and direction to mousepointCamera ca;if (cam != null ) ca = cam.GetComponent<Camera> (); else ca = Camera.main;Ray ray = ca.ScreenPointToRay(Input.mousePosition);//Return the ray's hitsRaycastHit[] hits = Physics.RaycastAll (ray);foreach (RaycastHit hit in hits) {print (hit.transform.gameObject.name);if (hit.collider.gameObject.tag.Contains("Finish")) { //plane tagDebug.Log ("hit " + hit.collider.gameObject.name +"!" ); }Destroy (hit.transform.gameObject);}} } }成品圖:
總結(jié)
以上是生活随笔為你收集整理的unity3d 飞碟游戏的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RabbitMQ实现延迟消息
- 下一篇: AHU计科(伪)新生指南