EasyTouch学习笔记一(控制主角的移动)

先学习下Easytouch的摇杆和按钮(先来一个官方实例,控制人物的移动)

 

EasyTouch学习笔记一(控制主角的移动)_第1张图片

 

1. 添加一个摇杆 Tools -> Hedgehog Team -> EasyTouch -> extensions -> adding a new jokstick

2. 添加一个摇杆 Tools -> Hedgehog Team -> EasyTouch -> extensions -> adding a new Button

3. 选中摇杆设置:Interaction Type: Event Notification 类型(否者你订阅的事件不会起作用,比如按钮的移动,开始等等)  

4. 射击按钮点击,触发MyPlayer类的Fire方法

EasyTouch学习笔记一(控制主角的移动)_第2张图片

MyPlayer.cs类(只有这个类,我就不上传源码了)

using UnityEngine;
using System.Collections;

public class MyPlayer : MonoBehaviour {

    public GameObject bullet;           //子弹
    private Transform model;            //主角人物
    private Transform gun;              //子弹发射的地点

    private Vector2 joystickOffect;     //按钮初始坐标
    private int initOffect = 0;

    // Use this for initialization
    void Start () {
        model = transform.Find("Model").transform;
        gun = transform.Find("Gun").transform;


        //注册事件
        EasyJoystick.On_JoystickMove += Move;
        EasyJoystick.On_JoystickMoveEnd += MoveEnd;
        EasyJoystick.On_JoystickMoveStart += MoveStart;
        
    }
    
    //开火
    void Fire()
    {
        Instantiate(bullet, gun.transform.position, gun.rotation);
    }

    void MoveStart(MovingJoystick move){}

    void OnDisable()
    {
        EasyJoystick.On_JoystickMove -= Move;
        EasyJoystick.On_JoystickMoveEnd -= MoveEnd;
    }

    void Move(MovingJoystick move)
    {
        //控制角色的移动
        float angle = move.Axis2Angle(true);
        transform.rotation = Quaternion.Euler(new Vector3(0,angle,0));
        transform.Translate(Vector3.forward * move.joystickValue.magnitude * Time.deltaTime);


        //如果手指超过遥感的边框,就跟随手指的方向移动
        if (move.joystickValue.magnitude == move.joystick.speed.x
            || move.joystickValue.magnitude == move.joystick.speed.y) 
        {
            //记录摇杆初始的x,y位置
            if (initOffect == 0) 
            {
                joystickOffect = move.joystick.JoystickPositionOffset;
                initOffect = 1;
            }
            //判断方向,
            move.joystick.JoystickPositionOffset = new Vector2(move.joystick.JoystickPositionOffset.x + move.joystickValue.x
                                                    , move.joystick.JoystickPositionOffset.y - move.joystickValue.y);
        }


        model.animation.CrossFade("Run");
    }

    void MoveEnd(MovingJoystick move)
    {
        move.joystick.JoystickPositionOffset = joystickOffect;      //让摇杆回到初始位置
        model.animation.CrossFade("idle");
    }




}

 

 

源码: http://yunpan.cn/cf4QyacmrRTUM  提取码 7c5a

 

新人求关照,有什么不对的地方,请大家留言(下次更新。。。。。)

 

你可能感兴趣的:(touch)