1. 程式人生 > 程式設計 >Unity3D實現人物移動示例

Unity3D實現人物移動示例

一個是通過W、A、S、D來移動人物(示例一),另個是按螢幕上的按鈕來移動人物(示例二)。很簡單,只改了幾行程式碼。

下面是“Assets”資料夾裡面的資源。

示例一:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class E3_07keyboard : MonoBehaviour
{
 //動畫陣列
 private Object[] animUp;
 private Object[] animDown;
 private Object[] animLeft;
 private Object[] animRight;
 //地圖貼圖
 private Texture2D map;
 //當前人物動畫
 private Object[] tex;
 //人物X座標
 private int x;
 //人物Y座標
 private int y;
 //幀序列
 private int nowFram;
 //動畫幀的總數
 private int mFrameCount;
 //限制一秒多少幀
 private float fps = 5;
 //限制幀的時間 
 private float time = 0;
 void Start()
 {
  //得到幀動畫中的所有圖片資源
  animUp = Resources.LoadAll("up");
  animDown = Resources.LoadAll("down");
  animLeft = Resources.LoadAll("left");
  animRight = Resources.LoadAll("right");
  //得到地圖資源
  map = (Texture2D)Resources.Load("map/map");
  //設定預設動畫
  tex = animUp;
 }
 
 void OnGUI()
 {
  //繪製貼圖
  GUI.DrawTexture(new Rect(0,Screen.width,Screen.height),map,ScaleMode.StretchToFill,true,0);
 
  //繪製幀動畫
  DrawAnimation(tex,new Rect(x,y,32,48));
 
  //點選按鈕移動人物
  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)
 {
  //繪製當前幀
  GUI.DrawTexture(rect,(Texture)tex[nowFram],0);
  //計算限制幀時間
  time += Time.deltaTime;
  //超過限制幀則切換圖片
  if (time >= 1.0 / fps)
  {
   //幀序列切換
   nowFram++;
   //限制幀清空
   time = 0;
   //超過幀動畫總數從第0幀開始
   if (nowFram >= tex.Length)
   {
    nowFram = 0;
   }
  }
 }
}

示例二

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class E3_07button : MonoBehaviour
{
 //動畫陣列
 private Object[] animUp;
 private Object[] animDown;
 private Object[] animLeft;
 private Object[] animRight;
 //地圖貼圖
 private Texture2D map;
 //當前人物動畫
 private Object[] tex;
 //人物X座標
 private int x;
 //人物Y座標
 private int y;
 //幀序列
 private int nowFram;
 //動畫幀的總數
 private int mFrameCount;
 //限制一秒多少幀
 private float fps = 5;
 //限制幀的時間 
 private float time = 0;
 void Start()
 {
  //得到幀動畫中的所有圖片資源
  animUp = Resources.LoadAll("up");
  animDown = Resources.LoadAll("down");
  animLeft = Resources.LoadAll("left");
  animRight = Resources.LoadAll("right");
  //得到地圖資源
  map = (Texture2D)Resources.Load("map/map");
  //設定預設動畫
  tex = animUp;
 }
 
 void OnGUI()
 {
  //繪製貼圖
  GUI.DrawTexture(new Rect(0,48));
 
  //點選按鈕移動人物
  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,0);
  //計算限制幀時間
  time += Time.deltaTime;
  //超過限制幀則切換圖片
  if (time >= 1.0 / fps)
  {
   //幀序列切換
   nowFram++;
   //限制幀清空
   time = 0;
   //超過幀動畫總數從第0幀開始
   if (nowFram >= tex.Length)
   {
    nowFram = 0;
   }
  }
 }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。