unity中摇杆的使用

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.EventSystems;

namespace JoystickPlugin

{

public class Joystick : MonoBehaviour, IPointerUpHandler, IDragHandler, IPointerDownHandler

{

public float Horizontal { get { return (snapX) ? SnapFloat(input.x, AxisOptions.Horizontal) : input.x; } }

public float Vertical { get { return (snapY) ? SnapFloat(input.y, AxisOptions.Vertical) : input.y; } }

public Vector2 Direction { get { return new Vector2(Horizontal, Vertical); } }

public float HandleRange

{

get { return handleRange; }

set { handleRange = Mathf.Abs(value); }

}

public float DeadZone

{

get { return deadZone; }

set { deadZone = Mathf.Abs(value); }

}

public AxisOptions AxisOptions { get { return AxisOptions; } set { axisOptions = value; } }

public bool SnapX { get { return snapX; } set { snapX = value; } }

public bool SnapY { get { return snapY; } set { snapY = value; } }

SerializeField\] private float handleRange = 1; \[SerializeField\] private float deadZone = 0; \[SerializeField\] private AxisOptions axisOptions = AxisOptions.Both; \[SerializeField\] private bool snapX = false; \[SerializeField\] private bool snapY = false; \[SerializeField\] public RectTransform background = null; \[SerializeField\] public RectTransform handle = null; private RectTransform baseRect = null; private Canvas canvas; private Camera cam; private Vector2 input = Vector2.zero; private void Awake() { HandleRange = 0.5f; DeadZone = 0; AxisOptions = AxisOptions.Both; SnapX = false; SnapY = false; background = transform.Find("Background").GetComponent\(); handle = transform.Find("Background/Handle").GetComponent\(); baseRect = GetComponent\(); canvas = transform.parent.GetComponent\(); // background = transform.GetComponent\(); Debug.Log("canvas:::" + canvas); Debug.Log("transform:::" + transform); // handle = transform.Find("Handle").GetComponent\(); } protected virtual void Start() { HandleRange = handleRange; DeadZone = deadZone; baseRect = GetComponent\(); canvas = GetComponentInParent\(); if (canvas == null) Debug.LogError("The Joystick is not placed inside a canvas"); Vector2 center = new Vector2(0.5f, 0.5f); background.pivot = center; handle.anchorMin = center; handle.anchorMax = center; handle.pivot = center; handle.anchoredPosition = Vector2.zero; input = Vector2.zero; } public virtual void OnPointerDown(PointerEventData eventData) { OnDrag(eventData); } public void OnDrag(PointerEventData eventData) { cam = null; if (canvas.renderMode == RenderMode.ScreenSpaceCamera) cam = canvas.worldCamera; Vector2 position = RectTransformUtility.WorldToScreenPoint(cam, background.position); Vector2 radius = background.sizeDelta / 2; input = (eventData.position - position) / (radius \* canvas.scaleFactor); FormatInput(); HandleInput(input.magnitude, input.normalized, radius, cam); handle.anchoredPosition = input \* radius \* handleRange; } protected virtual void HandleInput(float magnitude, Vector2 normalised, Vector2 radius, Camera cam) { if (magnitude \> deadZone) { if (magnitude \> 1) input = normalised; } else input = Vector2.zero; } private void FormatInput() { if (axisOptions == AxisOptions.Horizontal) input = new Vector2(input.x, 0f); else if (axisOptions == AxisOptions.Vertical) input = new Vector2(0f, input.y); } private float SnapFloat(float value, AxisOptions snapAxis) { if (value == 0) return value; if (axisOptions == AxisOptions.Both) { float angle = Vector2.Angle(input, Vector2.up); if (snapAxis == AxisOptions.Horizontal) { if (angle \< 22.5f \|\| angle \> 157.5f) return 0; else return (value \> 0) ? 1 : -1; } else if (snapAxis == AxisOptions.Vertical) { if (angle \> 67.5f \&\& angle \< 112.5f) return 0; else return (value \> 0) ? 1 : -1; } return value; } else { if (value \> 0) return 1; if (value \< 0) return -1; } return 0; } public virtual void OnPointerUp(PointerEventData eventData) { input = Vector2.zero; handle.anchoredPosition = Vector2.zero; } protected Vector2 ScreenPointToAnchoredPosition(Vector2 screenPosition) { Vector2 localPoint = Vector2.zero; if (RectTransformUtility.ScreenPointToLocalPointInRectangle(baseRect, screenPosition, cam, out localPoint)) { Vector2 pivotOffset = baseRect.pivot \* baseRect.sizeDelta; return localPoint - (background.anchorMax \* baseRect.sizeDelta) + pivotOffset; } return Vector2.zero; } } public enum AxisOptions { Both, Horizontal, Vertical } } using System.Collections; using System.Collections.Generic; using UnityEngine; using JoystickPlugin; public class testTANK : MonoBehaviour { FixedJoystick fx; public Transform t; // Start is called before the first frame update void Start() { //滑轮 fx = transform.Find("touch_bg").gameObject.AddComponent\(); fx.HandleRange = 0.5f; fx.enabled = true; } // Update is called once per frame void Update() { //Quaternion rotation = Quaternion.LookRotation(fx.Direction); // 应用旋转到GameObject //t.localEulerAngles =new Vector3(0,0, Vector2ToAngle(fx.Direction)); t.rotation = Quaternion.Euler(new Vector3(0, 0, Vector2ToAngle(fx.Direction))); //t.rotation = Quaternion.Euler(); t.transform.Translate(fx.Direction \* Time.fixedDeltaTime\*50 , Space.World); } float Vector2ToAngle(Vector2 vector) { // 使用Atan2计算角度,并将弧度转换为度数 float angle = Mathf.Atan2(vector.y, vector.x) \* Mathf.Rad2Deg-90f; // 保证角度为正数 if (angle \< 0) angle += 360; return angle; } }

相关推荐
scott.cgi13 小时前
Unity直接编译Java文件作为插件,导致失败的两个打包设置
java·unity·unity调用java·unity的java文件·unity的android插件·unity调用android·unity加载java代码
WiChP1 天前
【V0.1B9】从零开始的2D游戏引擎开发之路
c++·游戏引擎
游乐码1 天前
Unity坦克案例疑难记录(一)
unity·单例模式
小贺儿开发1 天前
Unity3D 编辑器对象锁定工具
unity·编辑器·编程·工具·对象·互动·拓展
AI前沿资讯1 天前
一站式 AI 3D 创作首选:V2Fun—— 直连 Unity + 多人动捕双核心,重塑轻量化生产管线
人工智能·3d·unity
winlife_2 天前
Unity 域重载会清空一切:Editor 工具如何让状态在重载后续命
unity·游戏引擎
深度森林2 天前
无人机“路径规划”高价值专利案例:基于抗干扰粒子群优化的无人机路径规划方法
游戏引擎·cocos2d
小贺儿开发2 天前
Unity3D 串口通信上位机联调系统
unity·串口·协议·数据·通信·传输·互动
tedcloud1232 天前
ppt-master部署教程:快速搭建智能演示文稿系统
服务器·人工智能·系统架构·游戏引擎·powerpoint
垂葛酒肝汤3 天前
Unity的UI扫光效果Shader
ui·unity·游戏引擎