EasyTouch5.0+Unity2017.2的摇杆控制角色移动,转向

EasyTouch5.0下载; gvro

点击打开链接

描述的不怎么样,不想看的直接 下工程看吧很简单;Unity2017.2点击打开链接 提取:3ngo

先看图:新建一个plan添加colider或者使用插件自带的地形,角色需要添加刚体和colider

EasyTouch5.0+Unity2017.2的摇杆控制角色移动,转向_第1张图片


右键添加摇杆

EasyTouch5.0+Unity2017.2的摇杆控制角色移动,转向_第2张图片

摇杆的名字在层级视图,F2或者点击是改不了名字的,不需要在属性面板 ETCjoystick 的Joystick name属性下修改:这个很重要,因为代码里可能会用到;

ETCJoystick脚本属性:

Axes properties 属性下的  Turn&Move direction Action 下的 水平和垂直 axis也可命名,除非有多个摇杆才会要求修改,

Direction acion(optional) 下的 Auto link on tag 勾选了可选择tag 物体目标作为旋转 移动什么的下面的action可改;

Camera属性类似的tag设置 可做一个摄像头跟随 角色移动的偏移设置,


简单的通过设置tag控制角色移动这种方式最简单设置垂直和水平轴属性的 Auto link on tag =Player就结束不需要写代码,但不好控制角色的移动状态;具体可参考官方的FPS dmeo 记得这样做必须要 角色 设置 tag=Player and 添加 Charactercontroller组件;

EasyTouch5.0+Unity2017.2的摇杆控制角色移动,转向_第3张图片



代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class JoyMove : MonoBehaviour {

    
    [SerializeField]
    private ETCJoystick joystick;//虚拟摇杆
    [SerializeField]
    private Rigidbody rig;

    [SerializeField]
    private float runSpeed = 2.0f;//移动速度
    [SerializeField]
    private Animation anim;

    void Start()
    {

        joystick.onMoveEnd.AddListener(() => onMoveEnd());

        //方式一:按键方法注册
        joystick.OnPressLeft.AddListener(() => JoystickHandlerMoving());
        joystick.OnPressRight.AddListener(() => JoystickHandlerMoving());
        joystick.OnPressUp.AddListener(() => JoystickHandlerMoving());
        joystick.OnPressDown.AddListener(() => JoystickHandlerMoving());

    }

    //方式二:输入监测
    private void Update()//加个s为了取消运行
    {
        if (ETCInput.GetAxisPressedUp("Vertical"))
        {
            JoystickHandlerMoving();
        }
       

        if (ETCInput.GetAxisPressedDown("Vertical"))
        {
            JoystickHandlerMoving();
        }
      

        if (ETCInput.GetAxisPressedLeft("Horizontal"))
        {
            JoystickHandlerMoving();

        }
        if (ETCInput.GetAxisPressedRight("Horizontal"))
        {
            JoystickHandlerMoving();

        }
       

    }
    void onMoveEnd()
    {
        anim.Play("soldierIdleRelaxed");
        rig.velocity = Vector3.zero;
    }


  
    void JoystickHandlerMoving()
    {      
        if (joystick.name != "New Joystick")
        {
            return;
        }

        //获取虚拟摇杆偏移量  
        float h = joystick.axisX.axisValue;
        float v = joystick.axisY.axisValue;

        if (Mathf.Abs(h) > 0.05f || (Mathf.Abs(v) > 0.05f))
        {
            Quaternion rota = transform.rotation;
            Quaternion finl = Quaternion.LookRotation(new Vector3(h, 0, v));
            transform.rotation = Quaternion.LerpUnclamped(rota, finl, 0.5f);
            rig.velocity = new Vector3(h * runSpeed, rig.velocity.y, v * runSpeed);
            anim.Play("soldierRun");
        }
        else
        {
            anim.Play("soldierIdleRelaxed");
        }
    }

   
}

你可能感兴趣的:(Unity插件,EasyTouch)