安装好Easy Touch插件之后直接在Hierarchy面板直接创建。
创建完成你就会看见一个EasyTouchControlsCanvas 这样就代表成功了
接下来就是如何用脚本控制虚拟摇杆如何控制人物移动
public ETCJoystick joystick;//虚拟摇杆
void Start()
{
//方式一:按键方法注册
joystick.onMoveEnd.AddListener(MoveEnd);
joystick.OnPressLeft.AddListener(JoystickHandlerMoving);
joystick.OnPressRight.AddListener(JoystickHandlerMoving);
joystick.OnPressUp.AddListener(JoystickHandlerMoving);
joystick.OnPressDown.AddListener(JoystickHandlerMoving);
});
//切记这里的joystick.name必须与你EasyTouchControlsCanvas这个下面的那个Joystick的名字统一默认的名字好像事new joystick;
public void JoystickHandlerMoving()
{
if (joystick.name != "MyJoystick"||playState==PlayState.Attack)
{
return;
}
//获取虚拟摇杆偏移量
float h = joystick.axisX.axisValue;
float v = joystick.axisY.axisValue;
if (Mathf.Abs(h) > 0.01f || (Mathf.Abs(v) > 0.01f))
{
Quaternion rota = transform.rotation;
Quaternion finl = Quaternion.LookRotation(new Vector3(h, 0, v));
transform.rotation = Quaternion.LerpUnclamped(rota, finl, 0.5f);
//这里的这个cc 我用的角色控制器CharactController
cc.Move(transform.forward * Time.deltaTime * runSpeed);
//transform.Translate(Vector3.forward * runSpeed * Time.deltaTime);
anim.Play("Run");
playState = PlayState.Moving;
}
}
附截图一张: