【问题标题】:Unity C# ScreenToWorldPoint 2D setting object Z axisunity C# ScreenToWorldPoint 2D设置对象Z轴
【发布时间】:2016-09-29 04:08:21
【问题描述】:
touchMarkerObjects[fingerId].transform.position = mainCamera.ScreenToWorldPoint(Input.GetTouch(fingerId).position);

当我使用这行代码时,它会将 touchMarkerObjects Z 位置设置为 -1308,从而使其无法渲染。相机位于 0,0,0 并设置为正交。即使我在之后添加一行或尝试仅使用 Vector3 中 ScreenToWorldPoint 的 X/Y 进行 transform.position 更改,它仍会将值设置为 -1308。

我应该如何在不影响 Z 索引的情况下使用它来更新对象位置?

谢谢,我确定这是我忽略的相对简单的事情。我希望这是传达问题的最简单方法。

【问题讨论】:

    标签: c# unity3d touch


    【解决方案1】:

    开始时,获取游戏对象的默认z 轴。 将 mainCamera.ScreenToWorldPoint(Input.GetTouch(fingerId).position); 中的值保存到临时变量中。在使用它之前,用该原始/默认变量覆盖结果的 z 轴。如果你的 GameObjects 的 z 轴是 0,简单地用 0 覆盖它。

    float defaultZ = 0f;
    

    或者

    float defaultZ = touchMarkerObjects[...].transform.position.z;
    

    然后

    Vector3 tempValue = mainCamera.ScreenToWorldPoint(Input.GetTouch(fingerId).position);
    tempValue.z = defaultZ;
    touchMarkerObjects[fingerId].transform.position = tempValue;
    

    【讨论】:

    • 非常感谢,这正是我想要的。我之前写了一些代码来默认 Z 值,并且肯定搞砸了。我很感激,这只是其中一个对我来说并不突出的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    • 2018-02-18
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多