【问题标题】:How to scale a gameObject with pinch?如何用捏缩放游戏对象?
【发布时间】:2020-05-09 14:25:49
【问题描述】:

如何在 Android/iOS 中用捏(2 根手指)缩放游戏对象(立方体)?如果夹点在扩大,那么立方体的比例 (x, y, z) 会变大,如果夹点在缩小,那么立方体的比例会变小。

我有一个用于鼠标拖动的脚本,但只能缩放 2 轴(x,y)。

public class Drag : MonoBehaviour
{
    Vector3 lastMousePosition;
    float scaleSensitivity = 2f;
    void Update()
    {
        if (Input.GetMouseButtonDown(0)) {
             lastMousePosition = Input.mousePosition;
         }
         if (Input.GetMouseButton(0)) {
             transform.localScale += ((Input.mousePosition - lastMousePosition) * Time.deltaTime * scaleSensitivity);
             lastMousePosition = Input.mousePosition;
         }
    }

}

【问题讨论】:

  • 看看 Input.Touches : docs.unity3d.com/ScriptReference/Input-touches.html 并使用差异 (Input.Touches[0] - Input.Touches[1]).magnitude,修复每一帧的差异。如果每帧都变大,则表示用户正在放大,否则如果它变小,则表示用户正在缩小。

标签: c# ios unity3d


【解决方案1】:

看看 Input.Touches : Input.Touches 并使用差异 (Input.Touches[0] - Input.Touches[1]).magnitude,修复每一帧的差异。如果每帧都变大,则表示用户正在放大,否则如果它变小,则表示用户正在缩小

float? _prevFrameDiff;

void Update()
{
    if (Input.touches.Length < 2)
    {
        _prevFrameDiff = null;
        return;
    }

    var diff = (Input.touches[0].position - Input.touches[1].position).magnitude;

    if (!_prevFrameDiff.HasValue)
    {
        _prevFrameDiff = diff;
        return;
    }
    else
    {
        var scaleFactor = diff / _prevFrameDiff.Value;

        this.transform.localScale = Vector3.one * scaleFactor;
    }
}

【讨论】:

  • 嘿,谢谢,但不幸的是它没有记录触摸的最后位置。我该怎么做?
猜你喜欢
  • 2019-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多