Easy Touch 摇感控制人物移动

public class joystick : MonoBehaviour

    {

        public float Speed;             //定义速度

        private CharacterController CC; //定义人称控制器

         //代码启动

        private void OnDisable()

        {

             //启动调用OnMove,OnMoveEnd

            EasyJoystick.On_JoystickMove += OnMove;

            EasyJoystick.On_JoystickMoveEnd += OnMoveEnd;

        }

        private void OnDestroy()

        {

             //结束关闭OnMove,OnMoveEnd

            EasyJoystick.On_JoystickMove -= OnMove;

            EasyJoystick.On_JoystickMoveEnd -= OnMoveEnd;

        }

        private void Start()

        {

            CC = GetComponent<CharacterController>();//得到控制器

        }

        //触摸移动方法

        public void OnMove(MovingJoystick move)

        {

             //判断是否是Easy Touch 摇感

            if (move.joystickName != “New joystick”)

            {

                return;

            }

             //定义前后左右移动

            float joyPositionX = move.joystickAxis.x;

            float joyPositionY = move.joystickAxis.y;

             //判断前后移动

            if (joyPositionY != 0 || joyPositionX != 0)

            {

                   //定义坐标

                Vector3 movePosition = new Vector3(joyPositionX, 0, joyPositionY);

                   //定义坐标增量

                Vector3 dir = transform.position + movePosition;

                   //面向方向

                transform.LookAt(dir);

                   //定位移动

                Vector3 moVector3 = transform.forward*Time.deltaTime*Speed;

                   //第一人称移动

                CC.Move(moVector3);

            }

        }

         //触摸移动方法结束

        public void OnMoveEnd(MovingJoystick move)

        {

           Debug.Log(“触摸抬起”)

        }

}

注:使用此方法,必须在导入Easy Touch插件之后进行,一般用于非PC端使用。

版权声明:本文为XiaoLang0原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/XiaoLang0/p/9560708.html