** 我开发的Unity游戏正在适配安卓平台**
玩家有挖掘范围, 还有虚拟指针, 以及虚拟摇杆来控制虚拟指针
** 问题相关代码如下**
//用的是我的UI创建方法, 所以这里直接用 new 代替了, cursorImage.rt 就是 image 的 rectTransform
void Start() => cursorImage = new Image();
void Update()
{
float max = tools.mainCamera.WorldToScreenPoint(new(transform.position.x + excavationRadius, transform.position.y)).x;
float min = -max;
#region 限制在范围内
if (cursorImage.rt.anchoredPosition.x > max)
cursorImage.rt.anchoredPosition = new(max, cursorImage.rt.anchoredPosition.y);
else if (cursorImage.rt.anchoredPosition.x < min)
cursorImage.rt.anchoredPosition = new(min, cursorImage.rt.anchoredPosition.y);
if (cursorImage.rt.anchoredPosition.y > max)
cursorImage.rt.anchoredPosition = new(cursorImage.rt.anchoredPosition.x, max);
else if (cursorImage.rt.anchoredPosition.y < min)
cursorImage.rt.anchoredPosition = new(cursorImage.rt.anchoredPosition.x, min);
#endregion
}
** 我想要达到的结果是**
虚拟指针可以在挖掘范围内移动, 并且移动边界是一个圆不是正方形
你的界限判断应该是与你设定的圆心点做距离判断,而不得单纯的判断点上的x,y的范围,类似 (x-x0)(x-x0)+(y-y0)(y-y0)<r*r
世界坐标转换为屏幕坐标,直接赋值给anchoredPosition3d应该就可以了,你现在这么写出现了什么现象啊?坐标转换错误了,还是位置错乱?