開發環境
Window7
Unity3D 3.4.1
MB525defy
Android 2.2.1
雖然標題是游戲開發,其實也是羽化測試鼠標和彈幕的時候做的一個小游戲,於是一點點有模有樣了,至於為什麼又是方塊,囧!所以就分享出來,內容很簡陋,代碼很簡單,先送上效果截圖。。。Android中PlanA是二刀流版- -大家湊合看吧-0-
本次學習:
1. 彈幕追蹤簡單AI
2. Unity鼠標特效
1. 彈幕追蹤簡單AI
群裡面有人共享的一個網頁彈幕代碼,通過XML控制,做得真的很不錯,這裡的彈幕AI很簡單,可以用到很多飛行游戲中,如果想做出花哨的子彈軌跡效果,這個要花很多時間在上面鑽研了,這裡的代碼只能實現前期的軌跡與方位追蹤。希望能給大家參考~ ~
Boss.js
- var target : Transform;
- var speed : float = 4.0;
- var LookAt : boolean = true;
- var go : boolean = false;
- private var mx : float;
- private var my : float;
- private var mz : float;
- private var dis : float;
- private var save : Vector3;
- private var time : int;
-
- function Start()
- {
- save = transform.position;
- dis = Random.value * 8- 4;
- mx = Random.value * 8 - 4;
- my = Random.value * 8 - 4;
- mz = Random.value * 8 - 4;
- time = 30 - Random.value*30;
- yield WaitForSeconds (time);
- go = true;
- }
-
- function Update ()
- {
- if(go)
- {
- if(Vector3.Distance(target.position, transform.position) > 5 && LookAt)
- {
- transform.LookAt(target.position);
- }
- else if(Vector3.Distance(target.position, transform.position) < 5)
- {
- LookAt = false;
- Destroy(gameObject,2);
- }
- transform.Translate(Vector3.forward * Time.deltaTime*speed);
- }
- else
- {
- transform.RotateAround(Vector3(mx,my,mz),save + Vector3(mx,my,0),Time.deltaTime * speed * dis);
- }
- }
2. Unity鼠標特效
上篇提過鼠標特效的事情,於是羽化就做了測試,由於現在公司游戲主攻Web端,手機端在此之上刪減,所以操作性質就發生了改變,但羽化還是寫了手機端的代碼,還是二刀流- -,圖片從晚上找的,這裡提供了兩種鼠標特效,當然在真正做的時候羽化估計會用第二種加強版,所以看大家的平台和需求來。MOUSE.js
- var target1 : Transform;
- var target1C : Transform;
- var target2 : Transform;
- var target2C : Transform;
- var mousePos1 : Vector3;
- var mousePos2 : Vector3;
- var cursorImage : Texture;
- var Mouse : GUISkin;
- private var MouseImg : boolean = false;
-
- function Update()
- {
- if(Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
- {
- if(Input.touchCount == 1)
- {
- mousePos1 = Input.touches[0].position;
- }
- else if(Input.touchCount == 2)
- {
- mousePos1 = Input.touches[0].position;
- mousePos2 = Input.touches[1].position;
- }
- }
- else
- {
- mousePos1 = Input.mousePosition;
- }
- target1.position = camera.ScreenToWorldPoint (Vector3(mousePos1.x,mousePos1.y,1));
- target2.position = camera.ScreenToWorldPoint (Vector3(mousePos2.x,mousePos2.y,1));
- }
-
- function LateUpdate()
- {
- if(Input.GetKey(KeyCode.Escape))
- {
- Application.Quit();
- }
- }
-
- function OnGUI()
- {
- if(MouseImg)
- {
- GUI.skin = Mouse;
- var windowRect : Rect = Rect (mousePos1.x - cursorImage.width/2, Screen.height - mousePos1.y - cursorImage.height/2, cursorImage.width, cursorImage.height);
- windowRect = GUI.Window (0, windowRect, DoMyWindow, "My Window");
- }
-
- if(GUILayout.Button("PlanA"))
- {
- Screen.showCursor = !Screen.showCursor;
- target1.gameObject.active = !target1.gameObject.active;
- target1C.gameObject.active = target1.gameObject.active;
- if(Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
- {
- target2.gameObject.active = !target2.gameObject.active;
- target2C.gameObject.active = target2.gameObject.active;
- }
- }
- else if(GUILayout.Button("PlanB"))
- {
- Screen.showCursor = !Screen.showCursor;
- MouseImg = !MouseImg;
- }
- else if(GUILayout.Button("Restart"))
- {
- Application.LoadLevel(0);
- }
-
- if(GUI.Button(new Rect(Screen.width-120,Screen.height-40,120,30),"Click to YUHUA!"))
- {
- Application.OpenURL("http://blog.csdn.net/libeifs");
- }
-
- GUI.color = Color.white;
- GUILayout.Label("fps:" + FPS.fps.ToString("f0") + " " + FPS.afps.ToString("f0"));
- }
-
- function DoMyWindow (windowID : int)
- {
- }
這裡第二種替換圖片,羽化用的是GUI中的Window,因為Window可以改變層級,大家研究下就知道,事件可以按需求自己判斷。若大家有什麼更好的點子,希望與羽化交流~ ~
Android Unity3D游戲開發之切割方塊源碼下載地址:
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /pub/Android源碼集錦/2011年/10月/Android Unity3D游戲開發之切割方塊源碼/