using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using System;
public class JoyStickController : MonoBehaviour,IDragHandler,IEndDragHandler,IBeginDragHandler{ public float maxDragDistance = 50f;
public Vector3 direction;
public Vector3 initPos;
public Vector3 dragPos;
// Use this for initialization
private static JoyStickController _instance; public static JoyStickController instance
{
get
{
return _instance;
}
}
public Vector3 GetDirction()
{
return direction.normalized;
}
void Awake()
{
_instance = this;
}
void Start () {
initPos = transform.position;
Debug.Log(initPos);
} // Update is called once per frame
void Update () { } public void OnBeginDrag(PointerEventData eventData)
{
//Debug.Log(Input.mousePosition);
dragPos = Input.mousePosition;
Debug.Log("OnBeginDrag");
} public void OnDrag(PointerEventData eventData)
{
float dis = Vector3.Distance(dragPos, Input.mousePosition);
direction = Input.mousePosition - dragPos;
if(dis>=maxDragDistance)
{ dis = maxDragDistance; }
transform.localPosition = direction.normalized * dis; Debug.Log("OnDrag"); }
public void OnEndDrag(PointerEventData eventData)
{
this.transform.position = initPos;
direction = Vector3.zero; Debug.Log("OnEndDrag");
Debug.Log(transform.position); }
}

移动处理

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class HatControl : MonoBehaviour
{
public GameObject effect;
public float fMoveSpeed = 5.0f;
private Vector3 rawPosition;
private Vector3 hatPosition;
private float fMaxWidth; private Vector3 deltaPos;
// Start is called before the first frame update
void Start()
{
Vector3 sceenPos = new Vector3(Screen.width, 0, 0);
Vector3 moveWidth = Camera.main.ScreenToWorldPoint(sceenPos);
float fBallWidth = GetComponent<Renderer>().bounds.extents.x; fMaxWidth = moveWidth.x - fBallWidth; deltaPos = Camera.main.transform.position - transform.position;
} private void Update()
{
Vector3 headPos = gameObject.transform.localPosition;
Vector3 normal = JoyStickController.instance.GetDirction(); float tickTime = Time.deltaTime * fMoveSpeed;
gameObject.transform.localPosition = new Vector3(headPos.x + normal.x * tickTime, headPos.y + normal.y * tickTime, headPos.z); Camera.main.transform.position = transform.position + deltaPos;
} private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.CompareTag("ball"))
{
GameObject newEffect = Instantiate(effect, transform.position, transform.rotation);
newEffect.transform.parent = transform; Destroy(newEffect, 1.0f);
Destroy(collision.gameObject);
} }
}

demo下载地址:

链接:https://pan.baidu.com/s/1sQBm2dPJLEUFB5A5zWPQ2A  密码:3jcg

Unity3D 移动摇杆处理的更多相关文章

  1. Unity3D 响应摇杆

    if (Input.GetKeyUp(KeyCode.Joystick1Button0)) { Debug.Log("Joystick1Button0"); } if (Input ...

  2. [Unity3D]Unity3D游戏开发之使用EasyTouch虚拟摇杆控制人物移动

    大家好,欢迎大家关注我的博客,我是秦元培,我的博客地址是blog.csdn.net/qinyuanpei.今天呢,我们来一起学习在Unity3D中使用EasyTouch虚拟摇杆来控制人物移动.虽然Un ...

  3. Unity3D学习(三):利用NGUI实现一个简单的左右摇杆

    前言 小游戏Konster在测试的时候有热心玩家反馈左右移动手感不是很好,最主要的问题是:手指一旦按在手机屏幕的一个方向按钮上,向反方向滑动到另一个方向按钮上是不会改变玩家移动方向的. 具体如下图: ...

  4. Unity3d项目入门之虚拟摇杆

    Unity本身不提供摇杆的组件,开发者可以使用牛逼的EasyTouch插件或者应用NGUI实现相关的需求,下面本文通过Unity自身的UGUI属性,实现虚拟摇杆的功能. 主参考 <Unity:使 ...

  5. 【转载】Unity3D研究院之IOS自定义游戏摇杆与飞机平滑的移动

    移动开发游戏中使用到的触摸游戏摇杆在iPhone上是非常普遍的,毕竟是全触摸屏手机,今天MOMO 通过一个小例子和大家讨论Unity3D 中如何自定义一个漂亮的全触摸游戏摇杆.        值得高兴 ...

  6. Unity3D 摄像机的Transform通过摇杆输出的方向

    要解决的问题是:摄像机的方向不固定,当摇杆向前(0,1)推时,主角要往摄像机的朝向(忽略Y方向)走,当摇杆往右(1,0)推的时,主角朝摄像机的右方向 /// <summary> /// 摄 ...

  7. Unity3D学习笔记(二十三):事件接口、虚拟摇杆、层级管理和背包系统

    事件接口 IDragHandler(常用):鼠标按下拖动时执行(只要鼠标在拖动就一直执行) IDropHandler:对象拖动结束时,如果鼠标在物体的范围内,执行一次(依赖于IDragHandler存 ...

  8. unity3d easytouch计算摇杆旋转角度以及摇杆八方向控制角色

    在写第三人称控制的时候,一开始在电脑测试是用WASD控制角色 后来需要发布到手机上,于是就加了一个摇杆 键盘控制角色的代码已经写好了,角色八方向移动 如果按照传统的大众思路来控制的话,是达不到我想要的 ...

  9. Unity3d 摇杆奖励

    单个单元: publicclass RockerSingle : MonoBehaviour { // 枚举.类别 RockerType  rockerType; //是否有效,最上面的为无效,即为f ...

随机推荐

  1. Docker之数据卷Volume(七)

    一.简介   Docker数据卷(volume)机制.volume是存在于一个或多个容器中的特定文件或文件夹,这个目录以独立于联合文件系统的形式在宿主机中存在,并为数据的共享与持久化提供便利. 1)v ...

  2. zabbix之 zabbix server 跟 agent 更换ip地址

    描述: zabbix server端跟agent端更改 ip . 改完之后,相应配置文件 (zabbix_agentd.conf.zabbix_server.conf)的ip也进行了替换 但是依旧报错 ...

  3. HAproxy增加日志记录功能和自定义日志输出内容、格式

    http://blog.51cto.com/eric1/1854574 一.增加haproxy日志记录功能   1.1 由于数据分析的需要,我们必须打开haproxy日志,记录相关信息. 在配置前,我 ...

  4. 十五、springcloud(一)注册中心Eureka

    1.Eureka的基本架构 a.Eureka Server 提供服务注册和发现 b.Service Provider 服务提供方 将自身服务注册到Eureka,从而使服务消费方能够找到 c.Servi ...

  5. [C#]typeof,Gettype()和is的区别

    typeof 参数是一个类型名称,比如你自己编写的一个类 GetType()是类的方法,继承自object,返回该实例的类型 is 用来检测实例的兼容性(是否可以相互转换) 例: class Anim ...

  6. Oracle DBA最常用的269条命令

    1 运行SQLPLUS工具 sqlplus 2 以OS的默认身份连接 / as sysdba 3 显示当前用户名 show user 4 直接进入SQLPLUS命令提示符 sqlplus /nolog ...

  7. sublime text 3 vue 语法高亮

    1.下载文件 链接 https://github.com/vuejs/vue-syntax-highlight 2.sublime菜单栏->Preferences->Browse Pack ...

  8. [转][C#]文件流读取

    { internal static class FileUtils { public static string GetRelativePath(string absPath, string base ...

  9. 转的,具体 https://www.cnblogs.com/icyJ/p/FreeShare.html

    网络资源下载网址 屏幕录像.图像处理,汉化和破解版本很新.国内破解汉化:大眼仔旭 http://www.dayanzai.me/ 开发工具,系统,数据库 http://msdn.itellyou.cn ...

  10. vm虚拟机 模板机进行克隆导致centos 7.2 无法加载网卡

    问题描述:vm虚拟机 模板机进行克隆导致centos 7.2 无法加载网卡. 1.ifconfig 查看网卡状态 lo: flags=<UP,LOOPBACK,RUNNING> mtu i ...