這篇文章給大家介紹使用Unity3D怎么實(shí)現(xiàn)人物移動(dòng)效果,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供亭湖企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計(jì)制作、網(wǎng)站設(shè)計(jì)、成都h5網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為亭湖眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。
下面是“Assets”文件夾里面的資源。
示例一:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class E3_07keyboard : MonoBehaviour { //動(dòng)畫數(shù)組 private Object[] animUp; private Object[] animDown; private Object[] animLeft; private Object[] animRight; //地圖貼圖 private Texture2D map; //當(dāng)前人物動(dòng)畫 private Object[] tex; //人物X坐標(biāo) private int x; //人物Y坐標(biāo) private int y; //幀序列 private int nowFram; //動(dòng)畫幀的總數(shù) private int mFrameCount; //限制一秒多少幀 private float fps = 5; //限制幀的時(shí)間 private float time = 0; void Start() { //得到幀動(dòng)畫中的所有圖片資源 animUp = Resources.LoadAll("up"); animDown = Resources.LoadAll("down"); animLeft = Resources.LoadAll("left"); animRight = Resources.LoadAll("right"); //得到地圖資源 map = (Texture2D)Resources.Load("map/map"); //設(shè)置默認(rèn)動(dòng)畫 tex = animUp; } void OnGUI() { //繪制貼圖 GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), map, ScaleMode.StretchToFill, true, 0); //繪制幀動(dòng)畫 DrawAnimation(tex, new Rect(x, y, 32, 48)); //點(diǎn)擊按鈕移動(dòng)人物 if (Input.GetKey(KeyCode.W)) { y -= 2; tex = animUp; } if (Input.GetKey(KeyCode.S)) { y += 2; tex = animDown; } if (Input.GetKey(KeyCode.A)) { x -= 2; tex = animLeft; } if (Input.GetKey(KeyCode.D)) { x += 2; tex = animRight; } } void DrawAnimation(Object[] tex, Rect rect) { //繪制當(dāng)前幀 GUI.DrawTexture(rect, (Texture)tex[nowFram], ScaleMode.StretchToFill, true, 0); //計(jì)算限制幀時(shí)間 time += Time.deltaTime; //超過限制幀則切換圖片 if (time >= 1.0 / fps) { //幀序列切換 nowFram++; //限制幀清空 time = 0; //超過幀動(dòng)畫總數(shù)從第0幀開始 if (nowFram >= tex.Length) { nowFram = 0; } } } }
示例二
using System.Collections; using System.Collections.Generic; using UnityEngine; public class E3_07button : MonoBehaviour { //動(dòng)畫數(shù)組 private Object[] animUp; private Object[] animDown; private Object[] animLeft; private Object[] animRight; //地圖貼圖 private Texture2D map; //當(dāng)前人物動(dòng)畫 private Object[] tex; //人物X坐標(biāo) private int x; //人物Y坐標(biāo) private int y; //幀序列 private int nowFram; //動(dòng)畫幀的總數(shù) private int mFrameCount; //限制一秒多少幀 private float fps = 5; //限制幀的時(shí)間 private float time = 0; void Start() { //得到幀動(dòng)畫中的所有圖片資源 animUp = Resources.LoadAll("up"); animDown = Resources.LoadAll("down"); animLeft = Resources.LoadAll("left"); animRight = Resources.LoadAll("right"); //得到地圖資源 map = (Texture2D)Resources.Load("map/map"); //設(shè)置默認(rèn)動(dòng)畫 tex = animUp; } void OnGUI() { //繪制貼圖 GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), map, ScaleMode.StretchToFill, true, 0); //繪制幀動(dòng)畫 DrawAnimation(tex, new Rect(x, y, 32, 48)); //點(diǎn)擊按鈕移動(dòng)人物 if (GUILayout.RepeatButton("向上")) { y -= 2; tex = animUp; } if (GUILayout.RepeatButton("向下")) { y += 2; tex = animDown; } if (GUILayout.RepeatButton("向左")) { x -= 2; tex = animLeft; } if (GUILayout.RepeatButton("向右")) { x += 2; tex = animRight; } } void DrawAnimation(Object[] tex, Rect rect) { //繪制當(dāng)前幀 GUI.DrawTexture(rect, (Texture)tex[nowFram], ScaleMode.StretchToFill, true, 0); //計(jì)算限制幀時(shí)間 time += Time.deltaTime; //超過限制幀則切換圖片 if (time >= 1.0 / fps) { //幀序列切換 nowFram++; //限制幀清空 time = 0; //超過幀動(dòng)畫總數(shù)從第0幀開始 if (nowFram >= tex.Length) { nowFram = 0; } } } }
關(guān)于使用Unity3D怎么實(shí)現(xiàn)人物移動(dòng)效果就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
當(dāng)前標(biāo)題:使用Unity3D怎么實(shí)現(xiàn)人物移動(dòng)效果
轉(zhuǎn)載來(lái)源:http://chinadenli.net/article8/ppgeop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、關(guān)鍵詞優(yōu)化、面包屑導(dǎo)航、微信公眾號(hào)、域名注冊(cè)、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)