【问题标题】:Vector3.zero is buggedVector3.zero 被窃听
【发布时间】:2018-07-10 08:45:57
【问题描述】:

我有一个简单的库存系统,其中 Canvas 设置为屏幕空间 - 相机。 出于某种原因,将图标设置为 vector3.zero 会使它们转到其他位置。

这是一个例子:

第一张图片正在启动,一切正常。

第二张图片是被拖动的苹果,你可以看到位置是正确的

一旦掉下来,就可以看到苹果去了一个未知的地方。

这是 endDrag 的代码:

public void OnEndDrag(PointerEventData eventData)
{
    if (item.category != ITEM_CATEGORY.Voxel)
    {
        icon.transform.position = Vector3.zero;
    }   
    else
    {
        cube.transform.position = Vector3.zero;
    }
}

没有什么独特的。

这里是拖动事件:

public void OnDrag(PointerEventData eventData)
{
    if (item.category != ITEM_CATEGORY.Voxel)
    {
        Vector3 screenPoint = Input.mousePosition;
        screenPoint.z = 0.13f; //distance of the plane from the camera
        icon.transform.position = Camera.main.ScreenToWorldPoint(screenPoint);
    }
    else
    {
        Vector3 screenPoint = Input.mousePosition;
        screenPoint.z = 0.13f; //distance of the plane from the camera
        cube.transform.position = Camera.main.ScreenToWorldPoint(screenPoint);
    }
}

【问题讨论】:

  • “出于某种原因,将图标设置为 vector3.zero 时会使它们转到 somewhere else - 可能是原点?
  • origin 应该是 0,0,0,这是它们的正确位置
  • 什么更可能? vector3.zero 的实现中存在错误,实际上只是vector3(0, 0, 0) 的简写,或者您的代码中存在错误?
  • 你的代码是罪魁祸首。 Vector3 不能被“窃听”:Reference

标签: c# visual-studio unity3d


【解决方案1】:

您在 RectTransform 的检查器中看到的是对象的本地位置。但在代码中,您正在操作对象的世界位置。当您将对象的 世界位置 设置为 (0, 0, 0) 时,它的 本地位置 不太可能也将是 (0, 0, 0)。您的代码所做的实际上是将对象移动到游戏世界的原点。

出错的不是 Vector3.zero(这没有任何意义),而是您的代码。在您的 OnEndDrag 函数中,尝试重置对象的本地位置而不是其世界位置,如下所示:

cube.transform.localPosition = Vector3.zero;

当然是图标的同义词。

【讨论】:

    猜你喜欢
    • 2015-01-13
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多