嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元微信扫码支付:2 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class joystick : MonoBehaviour
{
// Start is called before the first frame update
public Transform stick;
public Canvas canvas;
public float max_R = 80.0f;
private Vector2 m_dir;//设置一个单位圆来做方向
public Vector2 dir {
get{
return m_dir;
}
}
void Start()
{
stick.localPosition = Vector2.zero;//松开就弹回原来的位置
m_dir = Vector2.zero;
}
// Update is called once per frame
void Update()
{
}
public void on_begin_drag() {
//Debug.Log("on_begin_drag");
}
public void on_end_drag(){
//Debug.Log("on_end_drag");
stick.localPosition = Vector2.zero;//松开就弹回原来的位置
m_dir = Vector2.zero;
}
//len maxr = xxnew = y / ynew -- > (x new, y new)
public void on_drag(){
//Debug.Log("on_drag");
//Input.mousePosition;//屏幕坐标
Vector2 pos;
RectTransformUtility.ScreenPointToLocalPointInRectangle(transform as RectTransform,
Input.mousePosition, canvas.worldCamera, out pos);
//stick.localPosition = pos;//鼠标到哪里 stick就跟到哪里 这个就是相对坐标
float len = pos.magnitude;//获取向量的模
m_dir.x = pos.x / len;//cos r
m_dir.y = pos.y / len;//sin r
if (len > max_R) {
pos.x = pos.x * max_R / len;
pos.y = pos.y * max_R / len;
}
stick.localPosition = pos;//鼠标到哪里 stick就跟到哪里 这个就是相对坐标
Debug.Log(pos.x "," pos.y);
}
}