C# Unity2D移动矩形变换

C# Unity2D移动矩形变换,c#,unity3d,C#,Unity3d,RectTransform位于“Space-Overlay”中绘制的画布HUD中,它实际上接收指针/触摸事件,它还将RectTransform放置在正确的位置,只是精灵没有移动 下面是我放在Update()函数中的代码: foreach (Touch t in Input.touches) { // check all the touches if (t.position.x >= Screen.width/2) { // if it's on the right half of

RectTransform位于“Space-Overlay”中绘制的画布HUD中,它实际上接收指针/触摸事件,它还将RectTransform放置在正确的位置,只是精灵没有移动

下面是我放在Update()函数中的代码:

foreach (Touch t in Input.touches) { // check all the touches
    if (t.position.x >= Screen.width/2) { // if it's on the right half of the screen
        if (t.phase == TouchPhase.Began) { // if has just began pressing
            aimCenter = t.position; // set position
            Vector2 pos = new Vector2 (aimCenter.x / (float)Screen.width,aimCenter.y / (float)Screen.height); // calculate anchor position
            actionHandler.SetPosition (pos.x - .08f, pos.y - .14f, pos.x + .08f, pos.y + .14f); // apply anchor position (the function just sets anchorMax and anchorMin, then sets offsets to 0
            actionHandler.gameObject.SetActive (true); // make visible
        }
    }
    if (canMove && t.position.x < Screen.width/2) { // same as above but for the left half of the screen
        if (t.phase == TouchPhase.Began) {
            movCenter = t.position;
            Vector2 pos = new Vector2 (t.position.x / Screen.width, t.position.y / Screen.height);
            directionHandler.SetPosition (pos.x - .08f, pos.y - .14f, pos.x + .08f, pos.y + .14f);
            directionHandler.gameObject.SetActive (true);
        }
    }
}
foreach(在Input.toucks中触摸t){//检查所有触摸
如果(t.position.x>=Screen.width/2){//如果它在屏幕的右半部分
if(t.phase==TouchPhase.begined){//if刚刚开始按
aimCenter=t.position;//设置位置
Vector2 pos=新的Vector2(aimCenter.x/(float)屏幕。宽度,aimCenter.y/(float)屏幕。高度);//计算锚定位置
actionHandler.SetPosition(位置x-.08f,位置y-.14f,位置x+.08f,位置y+.14f);//应用锚定位置(函数仅设置锚定位置x和锚定位置N,然后将偏移设置为0
actionHandler.gameObject.SetActive(true);//使可见
}
}
如果(canMove&&t.position.x
代码的最终目标是在手指开始触摸的位置显示2个精灵,并保持在那里直到手指出现在屏幕上。 代码第一次工作,没有问题,但是当我再次尝试触摸屏幕时,精灵仍然处于相同的精确位置,或者更好:它是在相同的“旧”位置绘制的,而从编辑器中,我可以看到锚的实际位置是正确的

我如何解决这个问题? 我还尝试使用
rectcransform.sizeDelta
并通过
.localPosition
设置位置,但我遇到了同样的问题

另外,当我尝试在屏幕的同一侧使用两个手指时,精灵实际上会在它应该画的地方画出来

如果你想尝试一个实时版本,这里有