使用EasyTouch控制人物移动

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gsm958708323/article/details/82767970

操作流程

  • 导入虚拟按钮
  • 设置虚拟按钮属性
  • 代码添加委托事件
  • 控制人物移动

具体步骤

1.导入插件自带的虚拟按钮
在这里插入图片描述
2.设置虚拟按钮属性
这里使用Interaction type 设置为Event Notification,其他方式自己测试。
在这里插入图片描述
3.代码添加委托事件

        EasyJoystick.On_JoystickMoveStart += On_JoystickMoveStart;
        EasyJoystick.On_JoystickMoveEnd += On_JoystickMoveEnd;
        EasyJoystick.On_JoystickMove += On_JoystickMove;

        EasyButton.On_ButtonDown += On_ButtonDown;

这里使用几个虚拟摇杆常用的事件,对应后面的委托函数。

    void On_JoystickMoveStart(MovingJoystick move)
    {
        Debug.Log("摇杆开始移动");
    }
    
    void On_JoystickMoveEnd(MovingJoystick move)
    {
        Debug.Log("摇杆移动结束");
    }
    
    void On_JoystickMove(MovingJoystick move)
    {
        Debug.Log("摇杆移动");
    }

4.控制人物移动
改进后的On_JoystickMove方法,控制CharacterController组件实现人物的移动。

    void On_JoystickMove(MovingJoystick move)
    {
        Debug.Log("摇杆移动");
        float h = msg.joystickAxis.x;
        float v = msg.joystickAxis.y;
        //Debug.Log(h + "     " + v);

        Vector3 dir = new Vector3(h, 0, v);

        transform.LookAt(dir + transform.position);
        m_CharaCtrl.SimpleMove(dir);
    }

猜你喜欢

转载自blog.csdn.net/gsm958708323/article/details/82767970