由于作者手頭上沒(méi)有現(xiàn)成的模型,所以我將在Unity3D 官網(wǎng)中下載官方提供的游戲DEMO 中的模型來(lái)使用。另外官方提供了很多Unity3D 游戲DEMO,與詳細(xì)的文檔??梢詭椭覀儗W(xué)習(xí)Unity.有興趣的盆友可以去看看哈。

創(chuàng)新互聯(lián)公司主要為客戶提供服務(wù)項(xiàng)目涵蓋了網(wǎng)頁(yè)視覺(jué)設(shè)計(jì)、VI標(biāo)志設(shè)計(jì)、成都營(yíng)銷網(wǎng)站建設(shè)、網(wǎng)站程序開(kāi)發(fā)、HTML5響應(yīng)式網(wǎng)站建設(shè)公司、手機(jī)網(wǎng)站制作、微商城、網(wǎng)站托管及網(wǎng)站維護(hù)、WEB系統(tǒng)開(kāi)發(fā)、域名注冊(cè)、國(guó)內(nèi)外服務(wù)器租用、視頻、平面設(shè)計(jì)、SEO優(yōu)化排名。設(shè)計(jì)、前端、后端三個(gè)建站步驟的完善服務(wù)體系。一人跟蹤測(cè)試的建站服務(wù)標(biāo)準(zhǔn)。已經(jīng)為火鍋店設(shè)計(jì)行業(yè)客戶提供了網(wǎng)站營(yíng)銷推廣服務(wù)。
下載頁(yè)面:http://unity3d.com/support/resources/
本章博文的目的是利用上一章介紹的游戲搖桿來(lái)控制人物模型的移動(dòng),與行走動(dòng)畫的播放。
如上圖所示Create中的文件夾male中存放著模型動(dòng)畫與貼圖等,這個(gè)應(yīng)該是美術(shù)提供給我們的。然后將整個(gè)male用鼠標(biāo)拖動(dòng)到左側(cè)3D世界中,通過(guò)移動(dòng),旋轉(zhuǎn),縮放將人物模型放置在一個(gè)理想的位置。右側(cè)紅框內(nèi)設(shè)置模型動(dòng)畫的屬性。
Animation
idle1 該模型默認(rèn)動(dòng)畫名稱為idle1
Animations
size 該模型動(dòng)畫的數(shù)量
Element 該模型的動(dòng)畫名稱
Play Automatically 是否自動(dòng)播放
Animation Physics 是否設(shè)置該模型物理碰撞
Animation Only if Visable 是否設(shè)置該模型僅自己顯示
給該模型綁定一個(gè)腳本Controller.cs 用來(lái)接收搖桿返回的信息更新模型動(dòng)畫。
Controller.cs
[代碼]c#/cpp/oc代碼:
| 002 | using System.Collections; |
| 006 | public class Controller : MonoBehaviour { |
| 009 | public const int HERO_UP= 0; |
| 010 | public const int HERO_RIGHT= 1; |
| 011 | public const int HERO_DOWN= 2; |
| 012 | public const int HERO_LEFT= 3; |
| 014 | //人物當(dāng)前行走方向狀態(tài) |
| 015 | public int state = 0; |
| 017 | //備份上一次人物當(dāng)前行走方向狀態(tài) |
| 019 | public int backState = 0; |
| 022 | public MPJoystick moveJoystick; |
| 024 | //這個(gè)方法只調(diào)用一次,在Start方法之前調(diào)用 |
| 025 | public void Awake() { |
| 029 | //這個(gè)方法只調(diào)用一次,在Awake方法之后調(diào)用 |
| 037 | //獲取搖桿控制的方向數(shù)據(jù) 上一章有詳細(xì)介紹 |
| 038 | float touchKey_x = moveJoystick.position.x; |
| 039 | float touchKey_y = moveJoystick.position.y; |
| 043 | if(touchKey_x == -1){ |
| 044 | setHeroState(HERO_LEFT); |
| 046 | }else if(touchKey_x == 1){ |
| 047 | setHeroState(HERO_RIGHT); |
| 051 | if(touchKey_y == -1){ |
| 052 | setHeroState(HERO_DOWN); |
| 054 | }else if(touchKey_y == 1){ |
| 055 | setHeroState(HERO_UP); |
| 058 | if(touchKey_x == 0 && touchKey_y ==0){ |
| 059 | //松開(kāi)搖桿后播放默認(rèn)動(dòng)畫, |
| 060 | //不穿參數(shù)為播放默認(rèn)動(dòng)畫。 |
| 067 | public void setHeroState(int newState) |
| 070 | //根據(jù)當(dāng)前人物方向 與上一次備份方向計(jì)算出模型旋轉(zhuǎn)的角度 |
| 071 | int rotateValue = (newState - state) * 90; |
| 072 | Vector3 transformValue = new Vector3(); |
| 075 | animation.Play("walk"); |
| 077 | //模型移動(dòng)的位移的數(shù)值 |
| 080 | transformValue = Vector3.forward * Time.deltaTime; |
| 083 | transformValue = -Vector3.forward * Time.deltaTime; |
| 086 | transformValue = Vector3.left * Time.deltaTime; |
| 090 | transformValue = -Vector3.left * Time.deltaTime; |
| 096 | transform.Rotate(Vector3.up, rotateValue); |
| 099 | transform.Translate(transformValue, Space.World); |
上一章介紹了javaScript腳本使用游戲搖桿的方法,本章MOMO告訴大家使用C#腳本來(lái)使用游戲搖桿,上面我用 Controller.cs C#腳本來(lái)接收系統(tǒng)提供的Joystick.js是肯定無(wú)法使用的,須要修改成.cs文件,我在國(guó)外的一個(gè)網(wǎng)站上看到了一個(gè)老外幫我們已經(jīng)修改了,那么我將他修改后的代碼貼出來(lái)方便大家學(xué)習(xí),有興趣的朋友可以研究研究。
MPJoystick.cs
[代碼]c#/cpp/oc代碼:
| 005 | * File: MPJoystick.cs |
| 007 | * Author: Chris Danielson of (monkeyprism.com) |
| 011 | // USED TO BE: Joystick.js taken from Penelope iPhone Tutorial |
| 015 | // Joystick creates a movable joystick (via GUITexture) that |
| 017 | // handles touch input, taps, and phases. Dead zones can control |
| 019 | // where the joystick input gets picked up and can be normalized. |
| 023 | // Optionally, you can enable the touchPad property from the editor |
| 025 | // to treat this Joystick as a TouchPad. A TouchPad allows the finger |
| 027 | // to touch down at any point and it tracks the movement relatively |
| 029 | // without moving the graphic |
| 032 | [RequireComponent(typeof(GUITexture))] |
| 034 | public class MPJoystick : MonoBehaviour |
| 040 | public Vector2 min = Vector2.zero; |
| 042 | public Vector2 max = Vector2.zero; |
| 045 | private static MPJoystick[] joysticks; // A static collection of all joysticks |
| 047 | private static bool enumeratedJoysticks = false; |
| 049 | private static float tapTimeDelta = 0.3f; // Time allowed between taps |
| 052 | public Vector2 position = Vector2.zero; |
| 054 | public Rect touchZone; |
| 056 | public Vector2 deadZone = Vector2.zero; // Control when position is output |
| 058 | public bool normalize = false; // Normalize output after the dead-zone? |
| 062 | private int lastFingerId = -1; // Finger last used for this joystick |
| 064 | private float tapTimeWindow; // How much time there is left for a tap to occur |
| 066 | private Vector2 fingerDownPos; |
| 068 | //private float fingerDownTime; |
| 070 | //private float firstDeltaTime = 0.5f; |
| 071 | private GUITexture gui; |
| 073 | private Rect defaultRect; // Default position / extents of the joystick graphic |
| 075 | private Boundary guiBoundary = new Boundary(); // Boundary for joystick graphic |
| 077 | private Vector2 guiTouchOffset; // Offset to apply to touch input |
| 079 | private Vector2 guiCenter; // Center of joystick |
| 082 | gui = (GUITexture)GetComponent(typeof(GUITexture)); |
| 083 | defaultRect = gui.pixelInset; |
| 085 | defaultRect.x += transform.position.x * Screen.width;// + gui.pixelInset.x; // - Screen.width * 0.5; |
| 087 | defaultRect.y += transform.position.y * Screen.height;// - Screen.height * 0.5; |
| 088 | transform.position = Vector3.zero; |
| 091 | // If a texture has been assigned, then use the rect ferom the gui as our touchZone |
| 095 | touchZone = defaultRect; |
| 099 | guiTouchOffset.x = defaultRect.width * 0.5f; |
| 101 | guiTouchOffset.y = defaultRect.height * 0.5f; |
| 102 | // Cache the center of the GUI, since it doesn't change |
| 104 | guiCenter.x = defaultRect.x + guiTouchOffset.x; |
| 106 | guiCenter.y = defaultRect.y + guiTouchOffset.y; |
| 107 | // Let's build the GUI boundary, so we can clamp joystick movement |
| 109 | guiBoundary.min.x = defaultRect.x - guiTouchOffset.x; |
| 111 | guiBoundary.max.x = defaultRect.x + guiTouchOffset.x; |
| 113 | guiBoundary.min.y = defaultRect.y - guiTouchOffset.y; |
| 115 | guiBoundary.max.y = defaultRect.y + guiTouchOffset.y; |
| 120 | public Vector2 getGUICenter() { |
| 127 | gameObject.active = false; |
| 129 | //enumeratedJoysticks = false; |
| 132 | private void ResetJoystick() { |
| 134 | gui.pixelInset = defaultRect; |
| 138 | position = Vector2.zero; |
| 140 | fingerDownPos = Vector2.zero; |
| 143 | private bool IsFingerDown() { |
| 145 | return (lastFingerId != -1); |
| 148 | public void LatchedFinger(int fingerId) { |
| 150 | // If another joystick has latched this finger, then we must release it |
| 152 | if ( lastFingerId == fingerId ) |
| 159 | if (!enumeratedJoysticks) { |
| 161 | // Collect all joysticks in the game, so we can relay finger latching messages |
| 163 | joysticks = (MPJoystick[])FindObjectsOfType(typeof(MPJoystick)); |
| 165 | enumeratedJoysticks = true; |
| 168 | int count = Input.touchCount; |
| 169 | if ( tapTimeWindow > 0 ) |
| 171 | tapTimeWindow -= Time.deltaTime; |
| 184 | for(int i = 0; i < count; i++) { |
| 186 | Touch touch = Input.GetTouch(i); |
| 188 | Vector2 guiTouchPos = touch.position - guiTouchOffset; |
| 189 | bool shouldLatchFinger = false; |
| 193 | if (touchZone.Contains(touch.position)) |
| 195 | shouldLatchFinger = true; |
| 199 | else if (gui.HitTest(touch.position)) { |
| 201 | shouldLatchFinger = true; |
| 204 | // Latch the finger if this is a new touch |
| 206 | if (shouldLatchFinger && (lastFingerId == -1 || lastFingerId != touch.fingerId )) { |
| 211 | lastFingerId = touch.fingerId; |
| 213 | //fingerDownPos = touch.position; |
| 215 | //fingerDownTime = Time.time; |
| 218 | lastFingerId = touch.fingerId; |
| 222 | // Accumulate taps if it is within the time window |
| 224 | if ( tapTimeWindow > 0 ) |
| 232 | tapTimeWindow = tapTimeDelta; |
| 235 | // Tell other joysticks we've latched this finger |
| 237 | //for ( j : Joystick in joysticks ) |
| 239 | foreach (MPJoystick j in joysticks) { |
| 243 | j.LatchedFinger( touch.fingerId ); |
| 248 | if ( lastFingerId == touch.fingerId ) { |
| 250 | // Override the tap count with what the iPhone SDK reports if it is greater |
| 252 | // This is a workaround, since the iPhone SDK does not currently track taps |
| 254 | // for multiple touches |
| 256 | if ( touch.tapCount > tapCount ) |
| 258 | tapCount = touch.tapCount; |
| 261 | // For a touchpad, let's just set the position directly based on distance from initial touchdown |
| 263 | position.x = Mathf.Clamp( ( touch.position.x - fingerDownPos.x ) / ( touchZone.width / 2 ), -1, 1 ); |
| 265 | position.y = Mathf.Clamp( ( touch.position.y - fingerDownPos.y ) / ( touchZone.height / 2 ), -1, 1 ); |
| 269 | // Change the location of the joystick graphic to match where the touch is |
| 271 | Rect r = gui.pixelInset; |
| 273 | r.x = Mathf.Clamp( guiTouchPos.x, guiBoundary.min.x, guiBoundary.max.x ); |
| 275 | r.y = Mathf.Clamp( guiTouchPos.y, guiBoundary.min.y, guiBoundary.max.y ); |
| 280 | if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) |
| 291 | // Get a value between -1 and 1 based on the joystick graphic location |
| 293 | position.x = ( gui.pixelInset.x + guiTouchOffset.x - guiCenter.x ) / guiTouchOffset.x; |
| 295 | position.y = ( gui.pixelInset.y + guiTouchOffset.y - guiCenter.y ) / guiTouchOffset.y; |
| 298 | // Adjust for dead zone |
| 300 | var absoluteX = Mathf.Abs( position.x ); |
| 302 | var absoluteY = Mathf.Abs( position.y ); |
| 306 | if (absoluteX < deadZone.x) { |
| 308 | // Report the joystick as being at the center if it is within the dead zone |
| 316 | // Rescale the output after taking the dead zone into account |
| 318 | position.x = Mathf.Sign( position.x ) * ( absoluteX - deadZone.x ) / ( 1 - deadZone.x ); |
| 321 | if (absoluteY < deadZone.y) { |
| 323 | // Report the joystick as being at the center if it is within the dead zone |
| 331 | // Rescale the output after taking the dead zone into account |
| 333 | position.y = Mathf.Sign( position.y ) * ( absoluteY - deadZone.y ) / ( 1 - deadZone.y ); |
導(dǎo)出 build and run 看看在iPhone 上的效果,通過(guò)觸摸游戲搖桿可以控制人物的上,下,左,右 ,左上,右上,左下,右下 8個(gè)方向的移動(dòng)啦,不錯(cuò)吧,哇咔咔~~
分享名稱:Unity3D游戲引擎之FBX模型載入與人物行走動(dòng)畫播放
文章出自:
http://fisionsoft.com.cn/article/dhijoid.html