日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Unity3D游戲引擎之FBX模型載入與人物行走動(dòng)畫播放

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

創(chuàng)新互聯(lián)公司主要為客戶提供服務(wù)項(xiàng)目涵蓋了網(wǎng)頁視覺設(shè)計(jì)、VI標(biāo)志設(shè)計(jì)、成都營銷網(wǎng)站建設(shè)、網(wǎng)站程序開發(fā)、HTML5響應(yīng)式網(wǎng)站建設(shè)公司手機(jī)網(wǎng)站制作、微商城、網(wǎng)站托管及網(wǎng)站維護(hù)、WEB系統(tǒng)開發(fā)、域名注冊(cè)、國內(nèi)外服務(wù)器租用、視頻、平面設(shè)計(jì)、SEO優(yōu)化排名。設(shè)計(jì)、前端、后端三個(gè)建站步驟的完善服務(wù)體系。一人跟蹤測試的建站服務(wù)標(biāo)準(zhǔn)。已經(jīng)為火鍋店設(shè)計(jì)行業(yè)客戶提供了網(wǎng)站營銷推廣服務(wù)。

下載頁面:http://unity3d.com/support/resources/  

本章博文的目的是利用上一章介紹的游戲搖桿來控制人物模型的移動(dòng),與行走動(dòng)畫的播放。

 

如上圖所示Create中的文件夾male中存放著模型動(dòng)畫與貼圖等,這個(gè)應(yīng)該是美術(shù)提供給我們的。然后將整個(gè)male用鼠標(biāo)拖動(dòng)到左側(cè)3D世界中,通過移動(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 用來接收搖桿返回的信息更新模型動(dòng)畫。

Controller.cs

[代碼]c#/cpp/oc代碼:

001using UnityEngine; 
002using System.Collections; 
003   
004   
005   
006public class Controller : MonoBehaviour { 
007       
008    //人物的行走方向狀態(tài) 
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; 
013       
014    //人物當(dāng)前行走方向狀態(tài) 
015    public int state = 0; 
016       
017    //備份上一次人物當(dāng)前行走方向狀態(tài) 
018    //這里暫時(shí)沒有用到 
019    public int backState = 0; 
020       
021    //游戲搖桿對(duì)象 
022    public MPJoystick moveJoystick;   
023       
024    //這個(gè)方法只調(diào)用一次,在Start方法之前調(diào)用 
025    public void Awake() { 
026           
027    } 
028       
029    //這個(gè)方法只調(diào)用一次,在Awake方法之后調(diào)用 
030    void Start () { 
031        state = HERO_DOWN; 
032    } 
033       
034       
035    void Update () { 
036       
037    //獲取搖桿控制的方向數(shù)據(jù) 上一章有詳細(xì)介紹   
038    float touchKey_x =  moveJoystick.position.x;   
039    float touchKey_y =  moveJoystick.position.y;   
040           
041         
042        
043    if(touchKey_x == -1){   
044       setHeroState(HERO_LEFT); 
045             
046    }else if(touchKey_x == 1){   
047       setHeroState(HERO_RIGHT); 
048             
049    }   
050        
051    if(touchKey_y == -1){   
052        setHeroState(HERO_DOWN); 
053    
054    }else if(touchKey_y == 1){   
055        setHeroState(HERO_UP);          
056    }   
057       
058    if(touchKey_x == 0 && touchKey_y ==0){ 
059        //松開搖桿后播放默認(rèn)動(dòng)畫, 
060        //不穿參數(shù)為播放默認(rèn)動(dòng)畫。 
061        animation.Play(); 
062    } 
063       
064           
065    } 
066       
067    public void setHeroState(int newState) 
068    { 
069           
070        //根據(jù)當(dāng)前人物方向 與上一次備份方向計(jì)算出模型旋轉(zhuǎn)的角度 
071        int rotateValue = (newState - state) * 90; 
072        Vector3 transformValue = new Vector3(); 
073           
074        //播放行走動(dòng)畫 
075        animation.Play("walk"); 
076           
077        //模型移動(dòng)的位移的數(shù)值 
078        switch(newState){ 
079            case HERO_UP: 
080                transformValue = Vector3.forward * Time.deltaTime; 
081            break;   
082            case HERO_DOWN: 
083                transformValue = -Vector3.forward * Time.deltaTime; 
084            break;   
085            case HERO_LEFT: 
086                transformValue = Vector3.left * Time.deltaTime; 
087                   
088            break;   
089            case HERO_RIGHT: 
090                transformValue = -Vector3.left * Time.deltaTime; 
091            break;               
092        } 
093           
094           
095        //模型旋轉(zhuǎn) 
096        transform.Rotate(Vector3.up, rotateValue); 
097           
098        //模型移動(dòng) 
099        transform.Translate(transformValue, Space.World); 
100           
101        backState = state; 
102        state = newState; 
103           
104    } 
105       
106}

上一章介紹了javaScript腳本使用游戲搖桿的方法,本章MOMO告訴大家使用C#腳本來使用游戲搖桿,上面我用 Controller.cs  C#腳本來接收系統(tǒng)提供的Joystick.js是肯定無法使用的,須要修改成.cs文件,我在國外的一個(gè)網(wǎng)站上看到了一個(gè)老外幫我們已經(jīng)修改了,那么我將他修改后的代碼貼出來方便大家學(xué)習(xí),有興趣的朋友可以研究研究。 

MPJoystick.cs

 [代碼]c#/cpp/oc代碼:

001using UnityEngine;  
002   
003/**
004  
005 * File: MPJoystick.cs
006  
007 * Author: Chris Danielson of (monkeyprism.com)
008  
009 * 
010  
011// USED TO BE: Joystick.js taken from Penelope iPhone Tutorial
012  
013//
014  
015// Joystick creates a movable joystick (via GUITexture) that 
016  
017// handles touch input, taps, and phases. Dead zones can control
018  
019// where the joystick input gets picked up and can be normalized.
020  
021//
022  
023// Optionally, you can enable the touchPad property from the editor
024  
025// to treat this Joystick as a TouchPad. A TouchPad allows the finger
026  
027// to touch down at any point and it tracks the movement relatively 
028  
029// without moving the graphic
030  
031*/ 
032[RequireComponent(typeof(GUITexture))] 
033   
034public class MPJoystick : MonoBehaviour 
035   
036
037   
038class Boundary { 
039   
040public Vector2 min = Vector2.zero; 
041   
042public Vector2 max = Vector2.zero; 
043   
044
045private static MPJoystick[] joysticks;   // A static collection of all joysticks 
046   
047private static bool enumeratedJoysticks = false; 
048   
049private static float tapTimeDelta = 0.3f;    // Time allowed between taps 
050public bool touchPad; 
051   
052public Vector2 position = Vector2.zero; 
053   
054public Rect touchZone; 
055   
056public Vector2 deadZone = Vector2.zero;  // Control when position is output 
057   
058public bool normalize = false; // Normalize output after the dead-zone? 
059   
060public int tapCount;      
061   
062private int lastFingerId = -1;   // Finger last used for this joystick 
063   
064private float tapTimeWindow;     // How much time there is left for a tap to occur 
065   
066private Vector2 fingerDownPos; 
067   
068//private float fingerDownTime; 
069   
070//private float firstDeltaTime = 0.5f; 
071private GUITexture gui; 
072   
073private Rect defaultRect;    // Default position / extents of the joystick graphic 
074   
075private Boundary guiBoundary = new Boundary();   // Boundary for joystick graphic 
076   
077private Vector2 guiTouchOffset;  // Offset to apply to touch input 
078   
079private Vector2 guiCenter;   // Center of joystick 
080void Start() { 
081   
082gui = (GUITexture)GetComponent(typeof(GUITexture)); 
083defaultRect = gui.pixelInset; 
084   
085defaultRect.x += transform.position.x * Screen.width;// + gui.pixelInset.x; // -  Screen.width * 0.5; 
086   
087        defaultRect.y += transform.position.y * Screen.height;// - Screen.height * 0.5; 
088transform.position = Vector3.zero; 
089if (touchPad) { 
090   
091// If a texture has been assigned, then use the rect ferom the gui as our touchZone 
092   
093if ( gui.texture ) 
094   
095touchZone = defaultRect; 
096   
097} else { 
098   
099guiTouchOffset.x = defaultRect.width * 0.5f; 
100   
101guiTouchOffset.y = defaultRect.height * 0.5f; 
102// Cache the center of the GUI, since it doesn't change 
103   
104guiCenter.x = defaultRect.x + guiTouchOffset.x; 
105   
106guiCenter.y = defaultRect.y + guiTouchOffset.y; 
107// Let's build the GUI boundary, so we can clamp joystick movement 
108   
109guiBoundary.min.x = defaultRect.x - guiTouchOffset.x; 
110   
111guiBoundary.max.x = defaultRect.x + guiTouchOffset.x; 
112   
113guiBoundary.min.y = defaultRect.y - guiTouchOffset.y; 
114   
115guiBoundary.max.y = defaultRect.y + guiTouchOffset.y; 
116   
117
118   
119
120public Vector2 getGUICenter() { 
121   
122return guiCenter; 
123   
124
125void Disable() { 
126   
127gameObject.active = false; 
128   
129//enumeratedJoysticks = false; 
130   
131
132private void ResetJoystick() { 
133   
134gui.pixelInset = defaultRect; 
135   
136lastFingerId = -1; 
137   
138position = Vector2.zero; 
139   
140fingerDownPos = Vector2.zero; 
141   
142
143private bool IsFingerDown() { 
144   
145return (lastFingerId != -1); 
146   
147
148public void LatchedFinger(int fingerId) { 
149   
150// If another joystick has latched this finger, then we must release it 
151   
152if ( lastFingerId == fingerId ) 
153   
154ResetJoystick(); 
155   
156
157void Update() { 
158   
159if (!enumeratedJoysticks) { 
160   
161// Collect all joysticks in the game, so we can relay finger latching messages 
162   
163joysticks = (MPJoystick[])FindObjectsOfType(typeof(MPJoystick)); 
164   
165enumeratedJoysticks = true; 
166   
167
168int count = Input.touchCount; 
169if ( tapTimeWindow > 0 ) 
170   
171tapTimeWindow -= Time.deltaTime; 
172   
173else 
174   
175tapCount = 0; 
176if ( count == 0 ) 
177   
178ResetJoystick(); 
179   
180else 
181   
182
183   
184for(int i = 0; i < count; i++) { 
185   
186Touch touch = Input.GetTouch(i); 
187   
188Vector2 guiTouchPos = touch.position - guiTouchOffset; 
189bool shouldLatchFinger = false; 
190   
191if (touchPad) { 
192   
193if (touchZone.Contains(touch.position)) 
194   
195shouldLatchFinger = true; 
196   
197
198   
199else if (gui.HitTest(touch.position)) { 
200   
201shouldLatchFinger = true; 
202   
203
204// Latch the finger if this is a new touch 
205   
206if (shouldLatchFinger && (lastFingerId == -1 || lastFingerId != touch.fingerId )) { 
207if (touchPad) { 
208   
209//gui.color.a = 0.15; 
210   
211lastFingerId = touch.fingerId; 
212   
213//fingerDownPos = touch.position; 
214   
215//fingerDownTime = Time.time; 
216   
217
218lastFingerId = touch.fingerId; 
219   
220    
221   
222// Accumulate taps if it is within the time window 
223   
224if ( tapTimeWindow > 0 ) 
225   
226tapCount++; 
227   
228else { 
229   
230tapCount = 1; 
231   
232tapTimeWindow = tapTimeDelta; 
233   
234
235// Tell other joysticks we've latched this finger 
236   
237//for (  j : Joystick in joysticks ) 
238   
239foreach (MPJoystick j in joysticks) { 
240   
241if (j != this)  
242   
243j.LatchedFinger( touch.fingerId ); 
244   
245
246   
247}    
248if ( lastFingerId == touch.fingerId ) { 
249   
250// Override the tap count with what the iPhone SDK reports if it is greater 
251   
252// This is a workaround, since the iPhone SDK does not currently track taps 
253   
254// for multiple touches 
255   
256if ( touch.tapCount > tapCount ) 
257   
258tapCount = touch.tapCount; 
259if ( touchPad ) { 
260   
261// For a touchpad, let's just set the position directly based on distance from initial touchdown 
262   
263position.x = Mathf.Clamp( ( touch.position.x - fingerDownPos.x ) / ( touchZone.width / 2 ), -1, 1 ); 
264   
265position.y = Mathf.Clamp( ( touch.position.y - fingerDownPos.y ) / ( touchZone.height / 2 ), -1, 1 ); 
266   
267} else { 
268   
269// Change the location of the joystick graphic to match where the touch is 
270   
271Rect r = gui.pixelInset; 
272   
273r.x =  Mathf.Clamp( guiTouchPos.x, guiBoundary.min.x, guiBoundary.max.x ); 
274   
275r.y =  Mathf.Clamp( guiTouchPos.y, guiBoundary.min.y, guiBoundary.max.y ); 
276   
277gui.pixelInset = r; 
278   
279
280if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) 
281   
282ResetJoystick(); 
283   
284
285   
286
287   
288
289if (!touchPad) { 
290   
291// Get a value between -1 and 1 based on the joystick graphic location 
292   
293position.x = ( gui.pixelInset.x + guiTouchOffset.x - guiCenter.x ) / guiTouchOffset.x; 
294   
295position.y = ( gui.pixelInset.y + guiTouchOffset.y - guiCenter.y ) / guiTouchOffset.y; 
296   
297
298// Adjust for dead zone 
299   
300var absoluteX = Mathf.Abs( position.x ); 
301   
302var absoluteY = Mathf.Abs( position.y ); 
303   
304    
305   
306if (absoluteX < deadZone.x) { 
307   
308// Report the joystick as being at the center if it is within the dead zone 
309   
310position.x = 0; 
311   
312
313   
314else if (normalize) { 
315   
316// Rescale the output after taking the dead zone into account 
317   
318position.x = Mathf.Sign( position.x ) * ( absoluteX - deadZone.x ) / ( 1 - deadZone.x ); 
319   
320
321if (absoluteY < deadZone.y) { 
322   
323// Report the joystick as being at the center if it is within the dead zone 
324   
325position.y = 0; 
326   
327
328   
329else if (normalize) { 
330   
331// Rescale the output after taking the dead zone into account 
332   
333position.y = Mathf.Sign( position.y ) * ( absoluteY - deadZone.y ) / ( 1 - deadZone.y ); 
334   
335
336
337}

導(dǎo)出 build and run  看看在iPhone 上的效果,通過觸摸游戲搖桿可以控制人物的上,下,左,右 ,左上,右上,左下,右下 8個(gè)方向的移動(dòng)啦,不錯(cuò)吧,哇咔咔~~


分享文章:Unity3D游戲引擎之FBX模型載入與人物行走動(dòng)畫播放
瀏覽地址:http://www.5511xx.com/article/dhijoid.html