先學習下Easytouch的搖桿和按鈕(先來一個官方實例,控制人物的移動)
1. 添加一個搖桿 Tools -> Hedgehog Team -> EasyTouch -> extensions -> adding a new jokstick
2.添加一個搖桿 Tools -> Hedgehog Team -> EasyTouch -> extensions -> adding a new Button
3. 選中搖桿設置:Interaction Type: Event Notification 類型(否者你訂閱的事件不會起作用,比如按鈕的移動,開始等等)
4. 射擊按鈕點擊,觸發MyPlayer類的Fire方法
MyPlayer.cs類(只有這個類,我就不上傳源碼了)
using UnityEngine;using System.Collections;public class MyPlayer : MonoBehaviour { public GameObject bullet; //子彈 PRivate Transform model; //主角人物 private Transform gun; //子彈發射的地點 private Vector2 joystickOffect; //按鈕初始坐標 private int initOffect = 0; // Use this for initialization void Start () { model = transform.Find("Model").transform; gun = transform.Find("Gun").transform; //注冊事件 EasyJoystick.On_JoystickMove += Move; EasyJoystick.On_JoystickMoveEnd += MoveEnd; EasyJoystick.On_JoystickMoveStart += MoveStart; } //開火 void Fire() { Instantiate(bullet, gun.transform.position, gun.rotation); } void MoveStart(MovingJoystick move){} void OnDisable() { EasyJoystick.On_JoystickMove -= Move; EasyJoystick.On_JoystickMoveEnd -= MoveEnd; } void Move(MovingJoystick move) { //控制角色的移動 float angle = move.Axis2Angle(true); transform.rotation = Quaternion.Euler(new Vector3(0,angle,0)); transform.Translate(Vector3.forward * move.joystickValue.magnitude * Time.deltaTime); //如果手指超過遙感的邊框,就跟隨手指的方向移動 if (move.joystickValue.magnitude == move.joystick.speed.x || move.joystickValue.magnitude == move.joystick.speed.y) { //記錄搖桿初始的x,y位置 if (initOffect == 0) { joystickOffect = move.joystick.JoystickPositionOffset; initOffect = 1; } //判斷方向, move.joystick.JoystickPositionOffset = new Vector2(move.joystick.JoystickPositionOffset.x + move.joystickValue.x , move.joystick.JoystickPositionOffset.y - move.joystickValue.y); } model.animation.CrossFade("Run"); } void MoveEnd(MovingJoystick move) { move.joystick.JoystickPositionOffset = joystickOffect; //讓搖桿回到初始位置 model.animation.CrossFade("idle"); }}
源碼:http://yunpan.cn/cf4QyacmrRTUM 提取碼 7c5a
新人求關照,有什么不對的地方,請大家留言(下次更新。。。。。)
新聞熱點
疑難解答