3
我想确定用户以圆周运动的方式围绕屏幕旋转手指的方式。我目前正在尝试使用交叉产品并使用z组件来确定用户正在旋转的方式。这是产生的结果是在下半部分工作,并在上半部分旋转。确定手指旋转方向
任何人都可以阐明我做错了什么?
if ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved && GameStateManager.CurrentGameState == GameState.Minigame))
{
Vector3 touchPos = Vector3.zero;
if(Camera.mainCamera != null)
{
touchPos = Camera.mainCamera.ScreenToWorldPoint(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, Camera.mainCamera.nearClipPlane));
}
if(prevTouchPos == Vector2.zero)
{
prevTouchPos = new Vector2(touchPos.x, touchPos.y);
return;
}
//need angle between last finger position and this finger position
Vector2 prevVec = new Vector2(prevTouchPos.x - transform.position.x, prevTouchPos.y - transform.position.y);
Vector2 currVec = new Vector2(touchPos.x - transform.position.x, touchPos.y - transform.position.y);
float ang = Vector2.Angle(prevVec, currVec);
Vector3 cross = Vector3.Cross(new Vector3(prevVec.x, prevVec.y, 0), new Vector3(currVec.x, currVec.y, 0));
Debug.Log(cross.normalized);
if (cross.z < 0)
{
Debug.Log("Prev Vec: " + prevVec);
Debug.Log("Curr Vec: " + currVec);
Debug.Log("ROTATE RIGHT");
transform.Rotate(0, 0, ang);
}
else
{
Debug.Log("Prev Vec: " + prevVec);
Debug.Log("Curr Vec: " + currVec);
Debug.Log("ROTATE LEFT");
transform.Rotate(0, 0, -ang);
}
//Debug.Log(ang);
//Debug.Log("TouchPos: " + touchPos);
prevTouchPos = new Vector2(touchPos.x, touchPos.y);
}
我现在无法测试代码,无法绕过'Vector3.Cross'包装。我明白它做了什么以及你想做什么,但我无法测试它以确定发生了什么问题。您可以尝试在代码中的'ROTATE RIGHT/LEFT'之前添加'Debug.Log(“Crossvalue:”+ cross)“。这可能会让你对发生什么事情和发生错误有所了解。 – Joetjah
我认为你可以找到一个方法来做到这一点与 [此帖] [1] [1]:http://stackoverflow.com/questions/18579902/uiimage-which-follow-touches ++ –
可能重复[如何确定多边形点的列表是否按顺时针顺序?](http://stackoverflow.com/questions/1165647/how-to-determine-if-a-list -of-多边形点-是式时针顺序) –