简体   繁体   English

使用C#在Unity 3d中使用触摸旋转游戏对象

[英]Rotating gameobject using touch in Unity 3d with C#

I'm rotating a GameObject based on 2 finger touch. 我正在基于2指触摸旋转GameObject。 I have the rotate working fine, but I'm getting a strange issue where sometimes I will touch with two fingers to start the rotate, but as soon as I touch the screen the object will instantly rotate. 我的旋转工作正常,但是遇到一个奇怪的问题,有时我会用两根手指触摸以开始旋转,但是一旦触摸屏幕,对象就会立即旋转。 This seems to be random as to when it does it, and the rotation also appears to be random. 它何时执行似乎是随机的,并且轮换也似乎是随机的。 I'm thinking that the touch is triggering the rotate based on the last position, but my code should be resetting the start position. 我以为触摸会触发基于最后一个位置的旋转,但是我的代码应该重置开始位置。

The touch code is in a script attached to the object to rotate, so it's all in one file. 触摸代码位于附加到对象上的脚本中以进行旋转,因此全部都在一个文件中。

Here's the code. 这是代码。

In the Update method I detect that it's touch supported and attempt to call "HandleTouch()" 在Update方法中,我检测到它支持触摸,并尝试调用“ HandleTouch()”

void Update()
{
    if (Input.touchSupported)
        HandleTouch();
    else
        HandleMouse();
}

The HandleTouch method HandleTouch方法

private void HandleTouch()
{
    if (Input.touchCount < 2) return;
    switch (Input.touchCount)
    {
        case 2:
            Touch touch = Input.GetTouch(0);
            if (touch.phase == TouchPhase.Began)
            {
                lastRotPosition = touch.position;
            }
            else if (touch.phase == TouchPhase.Moved)
            {
                Vector3 offset = touch.position - lastRotPosition;
                lastRotPosition = touch.position;
                RotateCamera(offset.x * RotateSpeedTouch, offset.y * RotateSpeedTouch);
            }
            else if (touch.phase == TouchPhase.Ended)
            {
                lastRotPosition = new Vector2();
            }
            break;
    }
}

And this is the method that rotates the GameObject 这是旋转GameObject的方法

void RotateCamera(float x, float y)
{
    float rotX = x * rotateSpeed * Mathf.Deg2Rad;
    float rotY = y * rotateSpeed * Mathf.Deg2Rad;
    transform.Rotate(Vector3.up, rotX);
    transform.Rotate(Vector3.right, -rotY);
}

Thank you for any help or insight you might offer. 感谢您提供的任何帮助或见解。

You aren't tracking the fingerId 您没有跟踪fingerId

As touches may not be stored in the same order: 由于触摸可能不会以相同的顺序存储:

Furthermore, the continuity of a touch between frame updates can be detected by the device, so a consistent ID number can be reported across frames and used to determine how a particular finger is moving. 此外,设备可以检测到帧更新之间的触摸连续性,因此可以跨帧报告一致的ID号 ,并将其用于确定特定手指的移动方式。

...the fingerId property can be used to identify the same touch between frames. ... fingerId属性可用于识别帧之间的相同触摸。

So when a new finger touches the screen your code may see this: 因此,当新手指触摸屏幕时,您的代码可能会看到以下内容:

  • first finger touches, touch count 1, skip code 第一次手指触摸,触摸计数1,跳过代码
  • second finger touches, it is the second finger in the array, touch count 2, begin processing: 第二根手指触摸,它是数组中的第二根手指,触摸计数2,开始处理:
    • the first touch has the phase TouchPhase.Moved 第一次触摸的相位为TouchPhase.Moved
    • lastRotPosition is currently zero (due to either the default value or due to a previous TouchPhase.Ended ) lastRotPosition当前为零(由于默认值或由于先前的TouchPhase.Ended
    • touch.position - lastRotPosition evaluates to non-zero (big rotation occurs) touch.position - lastRotPosition计算结果为非零(发生大旋转)

When it works correctly you get this: 当它正常工作时,您得到以下信息:

  • first finger touches, touch count 1, skip code 第一次手指触摸,触摸计数1,跳过代码
  • second finger touches, it is the first finger in the array, touch count 2, begin processing: 第二个手指触摸,它是数组中的第一个手指,触摸计数2,开始处理:
    • the first touch has the phase TouchPhase.Begin 第一次触摸的相位为TouchPhase.Begin
    • lastRotPosition is updated to the touch's location lastRotPosition已更新为触摸的位置
    • next frame the touch.position - lastRotPosition evaluates to near-zero (small rotation occurs) 下一帧touch.position - lastRotPosition评估为接近零(发生小的旋转)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM