Unity3D實(shí)現(xiàn)人物移動(dòng)示例
一個(gè)是通過(guò)W、A、S、D來(lái)移動(dòng)人物(示例一),另個(gè)是按屏幕上的按鈕來(lái)移動(dòng)人物(示例二)。很簡(jiǎn)單,只改了幾行代碼。
下面是“Assets”文件夾里面的資源。

示例一:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class E3_07keyboard : MonoBehaviour
{
//動(dòng)畫(huà)數(shù)組
private Object[] animUp;
private Object[] animDown;
private Object[] animLeft;
private Object[] animRight;
//地圖貼圖
private Texture2D map;
//當(dāng)前人物動(dòng)畫(huà)
private Object[] tex;
//人物X坐標(biāo)
private int x;
//人物Y坐標(biāo)
private int y;
//幀序列
private int nowFram;
//動(dòng)畫(huà)幀的總數(shù)
private int mFrameCount;
//限制一秒多少幀
private float fps = 5;
//限制幀的時(shí)間
private float time = 0;
void Start()
{
//得到幀動(dòng)畫(huà)中的所有圖片資源
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)畫(huà)
tex = animUp;
}
void OnGUI()
{
//繪制貼圖
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), map, ScaleMode.StretchToFill, true, 0);
//繪制幀動(dòng)畫(huà)
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;
//超過(guò)限制幀則切換圖片
if (time >= 1.0 / fps)
{
//幀序列切換
nowFram++;
//限制幀清空
time = 0;
//超過(guò)幀動(dòng)畫(huà)總數(shù)從第0幀開(kāi)始
if (nowFram >= tex.Length)
{
nowFram = 0;
}
}
}
}

示例二
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class E3_07button : MonoBehaviour
{
//動(dòng)畫(huà)數(shù)組
private Object[] animUp;
private Object[] animDown;
private Object[] animLeft;
private Object[] animRight;
//地圖貼圖
private Texture2D map;
//當(dāng)前人物動(dòng)畫(huà)
private Object[] tex;
//人物X坐標(biāo)
private int x;
//人物Y坐標(biāo)
private int y;
//幀序列
private int nowFram;
//動(dòng)畫(huà)幀的總數(shù)
private int mFrameCount;
//限制一秒多少幀
private float fps = 5;
//限制幀的時(shí)間
private float time = 0;
void Start()
{
//得到幀動(dòng)畫(huà)中的所有圖片資源
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)畫(huà)
tex = animUp;
}
void OnGUI()
{
//繪制貼圖
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), map, ScaleMode.StretchToFill, true, 0);
//繪制幀動(dòng)畫(huà)
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;
//超過(guò)限制幀則切換圖片
if (time >= 1.0 / fps)
{
//幀序列切換
nowFram++;
//限制幀清空
time = 0;
//超過(guò)幀動(dòng)畫(huà)總數(shù)從第0幀開(kāi)始
if (nowFram >= tex.Length)
{
nowFram = 0;
}
}
}
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Unity3D實(shí)現(xiàn)攝像機(jī)鏡頭移動(dòng)并限制角度
- Unity3D實(shí)現(xiàn)控制攝像機(jī)移動(dòng)
- Unity鍵盤(pán)WASD實(shí)現(xiàn)物體移動(dòng)
- Unity3D實(shí)現(xiàn)虛擬按鈕控制人物移動(dòng)效果
- Unity3D實(shí)現(xiàn)物體旋轉(zhuǎn)縮放移動(dòng)效果
- Unity實(shí)現(xiàn)物體左右移動(dòng)效果
- Unity3D實(shí)現(xiàn)人物轉(zhuǎn)向與移動(dòng)
- unity實(shí)現(xiàn)UI元素跟隨3D物體
- unity實(shí)現(xiàn)攝像頭跟隨
- Unity實(shí)現(xiàn)主角移動(dòng)與攝像機(jī)跟隨
相關(guān)文章
C#中Winfrom默認(rèn)輸入法的設(shè)置方法
這篇文章主要介紹了C#中Winfrom默認(rèn)輸入法的設(shè)置方法,以實(shí)例形式較為詳細(xì)的分析了C#中輸入法設(shè)置的相關(guān)技巧,需要的朋友可以參考下2015-05-05
C#實(shí)現(xiàn)類(lèi)似新浪微博長(zhǎng)URL轉(zhuǎn)短地址的方法
這篇文章主要介紹了C#實(shí)現(xiàn)類(lèi)似新浪微博長(zhǎng)URL轉(zhuǎn)短地址的方法,涉及C#操作正則表達(dá)式的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
C# HttpClient 如何使用 Consul 發(fā)現(xiàn)服務(wù)
這篇文章主要介紹了C# HttpClient 如何使用 Consul 發(fā)現(xiàn)服務(wù),幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-02-02
C#中字符串與字節(jié)數(shù)組的轉(zhuǎn)換方式
這篇文章介紹了C#中字符串與字節(jié)數(shù)組的轉(zhuǎn)換方式,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
C#實(shí)現(xiàn)計(jì)算器功能(winform版)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)winform版的計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
C# 字符串與unicode互相轉(zhuǎn)換實(shí)戰(zhàn)案例
這篇文章主要介紹了C# 字符串與unicode互相轉(zhuǎn)換實(shí)戰(zhàn)案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
數(shù)字金額大寫(xiě)轉(zhuǎn)換器制作代碼分享(人民幣大寫(xiě)轉(zhuǎn)換)
一個(gè)人民幣大寫(xiě)的擴(kuò)展方法,可以做成數(shù)字金額大寫(xiě)轉(zhuǎn)換器,大家參考使用吧2013-12-12

