對於移動平臺上的RPG類的遊戲,咱們經常使用虛擬搖桿來控制人物角色的行走和一些行爲,相信咱們對它並不陌生,以前嘗試了EasyTouch2.5,發現並無最新版的3.1好用,2.5版本的對於自適應沒有作的很好,而最新版的已經解決了這一問題。固然unity也自帶了搖桿Joystick,用起來也簡單,但存在很多侷限,不會知足普通mmo遊戲的需求,好比指定顯示區域或者是更改一些素材等等,而這些EasyTouch插件都已經幫你實現,不得不佩服插件的原做者,能作出這麼炫酷好用的插件,固然這估計是老外開發的,關於插件的下載,你能夠在AssetStore上購買,固然也可使用free版。函數
我這裏也提供一個3.1版本的插件下載地址:http://download.csdn.net/detail/s10141303/6962919學習
感受很酷有木有!接下來就看一下建立的過程吧!動畫
Quick Start (C#)ui
1-Import EasyTouch Package.
2-Create an empty gameObject, and name it EasyTouch.(You can choose another name)
Step 1 & 2 can be replace by the option menuspa
3-Add the EasyTouch.cs script on the EasyTouch gameObject that you just created.
4-Select the EasyTouch gameobject, and verifies that Broadcast messages is set to FALSE in the inspector..net
5-Create a new C# script MyFirstTouch
6-Add these methods
插件
[csharp] view plaincopyprint?翻譯
// Subscribe to events code
void OnEnable(){ orm
EasyTouch.On_TouchStart += On_TouchStart;
}
// Unsubscribe
void OnDisable(){
EasyTouch.On_TouchStart -= On_TouchStart;
}
// Unsubscribe
void OnDestroy(){
EasyTouch.On_TouchStart -= On_TouchStart;
}
// Touch start event
public void On_TouchStart(Gesture gesture){
Debug.Log( "Touch in " + gesture.position);
}
7-Create an empty gameObject, and name it Receiver.
8- Add MyFirstTouch script to the gameObject Receiver.
9- Run it in editor, and click on the screen
1.import「EasyTouch」資源包
2.建立空物體,命名爲EasyTouch(固然你也能夠改爲其餘名字)
3.添加EasyTouch.cs腳本在剛剛建立的空物體(EasyTouch)上
4.選擇改物體但不要將BroadcastMessages勾選
5.建立一個新的C#腳本,命名MyFirstTouch
6.添加這些方法
7.再建立一個空物體,命名爲Receiver
8.將MyFirstTouch腳本添加到空物體Receiver上
9.運行而且點擊遙感,會發現控制檯打印了當前按下的座標
1.導入EasyTouch3資源包
2.作好前期準備,包括人物模型、地形的建立
3.添加JoyStick實例:Hedgehog Team->Easy Touch->Extensions->Add a new Joystick。此時就會在左下角建立了虛擬遙感的實例。
4.設置遙感的相關參數
5.建立腳本MoveController.cs用來接收遙感事件控制角色的移動
[csharp] view plaincopyprint?
using UnityEngine;
using System.Collections;
public class MoveController : MonoBehaviour {
void OnEnable()
{
EasyJoystick.On_JoystickMove += OnJoystickMove;
EasyJoystick.On_JoystickMoveEnd += OnJoystickMoveEnd;
}
//移動搖桿結束
void OnJoystickMoveEnd(MovingJoystick move)
{
//中止時,角色恢復idle
if (move.joystickName == "MoveJoystick")
{
animation.CrossFade("idle");
}
}
//移動搖桿中
void OnJoystickMove(MovingJoystick move)
{
if (move.joystickName != "MoveJoystick")
{
return;
}
//獲取搖桿中心偏移的座標
float joyPositionX = move.joystickAxis.x;
float joyPositionY = move.joystickAxis.y;
if (joyPositionY != 0 || joyPositionX != 0)
{
//設置角色的朝向(朝向當前座標+搖桿偏移量)
transform.LookAt(new Vector3(transform.position.x + joyPositionX, transform.position.y, transform.position.z + joyPositionY));
//移動玩家的位置(按朝向位置移動)
transform.Translate(Vector3.forward * Time.deltaTime * 5);
//播放奔跑動畫
animation.CrossFade("run");
}
}
}
幾個函數的執行順序:
6.效果圖
7.建立點擊按鈕
點擊HedgehogTeam->EasyTouch->Extensions->Create a new Button,會在屏幕右下角建立一個button
如何讓有下角的按鈕點擊能作出咱們想要的效果呢?
jump方法:
而後點擊一下按鈕,人物就會跳躍動做!
如需轉載請註明出處:http://blog.csdn.net/dingxiaowei2013/article/details/19967041
==================== 迂者 丁小未 CSDN博客專欄=================
MyBlog:http://blog.csdn.net/dingxiaowei2013 MyQQ:1213250243
Unity QQ羣:858550 cocos2dx QQ羣:280818155
====================== 相互學習,共同進步 ===================