1. 程式人生 > >EasyTouch 核心控制指令碼

EasyTouch 核心控制指令碼

using UnityEngine;  
using System.Collections;  
  
public class MoveController : MonoBehaviour {  
  
    //當搖桿可用時註冊事件  
    void OnEnable()  
    {  
        EasyJoystick.On_JoystickMove += OnJoystickMove;  
        EasyJoystick.On_JoystickMoveEnd += OnJoystickMoveEnd;  
    }  
  
    //當搖桿不可用時移除事件  
    void OnDisable()  
    {  
        EasyJoystick.On_JoystickMove -= OnJoystickMove;  
        EasyJoystick.On_JoystickMoveEnd -= OnJoystickMoveEnd;  
    }  
  
    //當搖桿銷燬時移除事件  
    void OnDestroy()  
    {  
        EasyJoystick.On_JoystickMove -= OnJoystickMove;  
        EasyJoystick.On_JoystickMoveEnd -= OnJoystickMoveEnd;  
    }  
  
    //當搖桿處於停止狀態時,角色進入待機狀態  
    void OnJoystickMoveEnd(MovingJoystick move)  
    {  
        if (move.joystickName == "EasyJoystick")  
        {  
            
        }  
    }  
  
    //當搖桿處於移動狀態時,角色開始奔跑  
    void OnJoystickMove(MovingJoystick move)  
    {  
        if (move.joystickName != "EasyJoystick")  
        {  
            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 * 7.5F);  
            
        }  
    }  
}