EasyJoystick使用

阴永逸
2023-12-01

方法1.
public class Cheromove1 : MonoBehaviour
 {
    public float speed = 3;
    public EasyJoystick _joystick;  //创建摇杆对象
    
    void Update ()
    {
        float x = _joystick.JoystickTouch.x;
        float y = _joystick.JoystickTouch.y;
        transform.LookAt (transform.position + new Vector3(x,0,y));  //控制角色正面朝向,本身位置加摇杆即可 

        if (x !=0 || y!=0)
        {
            //transform.Translate (Vector3.forward * speed * Time.deltaTime);
            transform.Translate (new Vector3 (0,0,speed*Time.deltaTime));
            GetComponent<Animation> ().CrossFade ("Run");
        }  else {
            GetComponent<Animation> ().CrossFade ("idle");
        }

    }
}
方法2.
public class Cheromove2 : MonoBehaviour
 {
    public float speed = 3f;   //只要拖动摇杆,触发事件,调用事件中的方法
 
    void OnEnable()            //当脚本激活后运行该方法,在start之前调用
    {
        EasyJoystick.On_JoystickMoveStart +=moveStart;
        EasyJoystick.On_JoystickMove +=moving;
        EasyJoystick.On_JoystickMoveEnd += moveEnd;
    }
    void moveStart (MovingJoystick move)
    {

    }
    void moving (MovingJoystick move)
    {
        //float x = move.joystick.JoystickTouch.x;
        //float y = move.joystick.JoystickTouch.y;

        float x = move.joystickAxis.x;
        float y = move.joystickAxis.y;
        transform.LookAt (transform.position + new Vector3 (x, 0, y));
        if (x!= 0 || y!= 0) 
        {
            transform.Translate (Vector3.forward * speed * Time.deltaTime);
            GetComponent<Animation> ().CrossFade ("Run");
        }
    }
    void moveEnd (MovingJoystick move) 
    {
        GetComponent<Animation> ().CrossFade ("idle");
    }
    void OnDisable()  //脚本变为不活跃状态
    {
        EasyJoystick.On_JoystickMoveStart -=moveStart;
        EasyJoystick.On_JoystickMove -=moving;
        EasyJoystick.On_JoystickMoveEnd -= moveEnd;
    }
    void OnDestroy()
    {
        EasyJoystick.On_JoystickMoveStart -=moveStart;
        EasyJoystick.On_JoystickMove -=moving;
        EasyJoystick.On_JoystickMoveEnd -= moveEnd;
    }
}
方法3.
   public EasyJoystick myjoystick;
   if (myjoystick.JoystickTouch != Vector2.zero) 
   {
       stateManager.ChangeState (FsmState.run);
       transform.LookAt (transform.position + new Vector3 (myjoystick.JoystickTouch.x,0,myjoystick.JoystickTouch.y));
       Vector3 dir = new Vector3 (myjoystick.JoystickTouch.x, 0, myjoystick.JoystickTouch.y);
       characterctr.SimpleMove (dir * speed *Time.deltaTime);
    }
 类似资料: