Unity3D EasyTouch JoyStick控制小黄人的移动

利用EasyTouch中的 JoyStick控制小黄人的移动
如下图所示:

Unity3D EasyTouch JoyStick控制小黄人的移动_第1张图片
Unity3D EasyTouch JoyStick控制小黄人的移动_第2张图片

挂载在EasyTouch上的ControlWithEasyTouch脚本:

using UnityEngine;
using System.Collections;

public class ControlWithEasyTouch : MonoBehaviour {

    #region variable
    public EasyJoystick myJoy;
    public Transform littleY;
    public float moveSpeed = 2.0f;
    #endregion

    void Update () {
        ControlMove();
    }

    /// 
    /// Controls the gameobject move.
    /// 
    void ControlMove() {
        float moveX = myJoy.JoystickTouch.x;
        float moveZ = myJoy.JoystickTouch.y;
        Vector3 dir = new Vector3(moveX, 0, moveZ).normalized;//找到方向并把方向归一化

        if (moveX != 0f || moveZ != 0f) {
            //Debug.Log(moveX+"----------"+moveZ);
            //转向方法一:
            //littleY.LookAt(littleY.position + dir);
            //转向方法二:
            Quaternion wantRotation = Quaternion.LookRotation(dir);
            Quaternion dumpRotation = Quaternion.Lerp(littleY.rotation, wantRotation, 10f * Time.deltaTime);
            littleY.localRotation = dumpRotation;
            //改变游戏角色对象的方向
            littleY.Translate(Vector3.back * moveSpeed * Time.deltaTime); //移动
        }
    }
}

你可能感兴趣的:(Unity3D)