所以,我正在尝试旋转一个对象。非常简单,只要触摸屏幕,就会创建一个 Vector3,获取触摸位置,然后当用户在屏幕上拖动手指时,它会进行计算以旋转对象。当用户将手指从屏幕上移开时,Vector3 会被破坏,并且一切正常。
在编辑器上,它完美无缺。然而,在 android 设备上,感觉就像设备记得最后被触摸的点在哪里。因此,用户第一次尝试旋转对象时没有问题,但是,如果他们想再次旋转它,脚本会计算用户上次点击的位置并旋转对象,就像用户一直拖动手指一样到新职位。
有什么原因吗? android设备是否存储触摸位置?如果是这样,有没有办法重置它?作为一个额外的问题,类似的事情也会发生在 iOS 设备上吗?
编辑:代码。
bool topSide;
bool rightSide;
public float rotationSpeed;
void OnMouseDrag()
{
if (Input.mousePosition.x > Screen.width / 2)
{
rightSide = true;
}
else
{
rightSide = false;
}
if (Input.mousePosition.y > Screen.height / 2)
{
topSide = true;
}
else
{
topSide = false;
}
if (rightSide)
{
rot1 = Input.GetAxis("Mouse Y") * rotationSpeed * Mathf.Deg2Rad;
}
else
{
rot1 = -Input.GetAxis("Mouse Y") * rotationSpeed * Mathf.Deg2Rad;
}
if (topSide)
{
rot2 = -Input.GetAxis("Mouse X") * rotationSpeed * Mathf.Deg2Rad;
}
else
{
rot2 = Input.GetAxis("Mouse X") * rotationSpeed * Mathf.Deg2Rad;
}
sundial.transform.localEulerAngles += new Vector3(0, 0, (rot1 + rot2) * rotationSpeed * Time.deltaTime);
rot1 = 0;
rot2 = 0;
}
答案 0 :(得分:1)
OnMouseDrag
函数可能仅适用于鼠标。我通常使用一些这样的代码来检测是否有东西被点击:
public void Update() {
if(Input.GetMouseButtonDown(0) || Input.GetMouseButton(0)) {
//raycast to object, check for collision
//then find angle and store last position, and move the object
} else if(Input.GetMouseButtonUp(0)) {
//erase last position and do whatever you do when the user picks up their finger
}
}
我意识到这也指的是鼠标按钮,但我到处都在使用它,而且它适用于移动/桌面平台。因此,如果您担心是平台问题,请尝试使用该方法!
它还涉及光线投射,您还没有这样做,但非常值得学习如何做到这一点!