【问题标题】:How to make my sprite object go offscreen to the left and have it appear on the right?如何让我的精灵对象离开屏幕到左边并让它出现在右边?
【发布时间】:2015-04-13 11:38:34
【问题描述】:

Unity 中,我试图让我的精灵离开屏幕到左侧并让它出现在右侧的屏幕外,反之亦然。顺便说一下,我的精灵不断向左或向右移动(取决于用户输入)以进行游戏。

我考虑到精灵需要完全离开屏幕才能出现在右侧。

这是我当前的代码:

void Start () 
{
    minXValueWorld = Camera.main.ScreenToWorldPoint(new Vector3(0,0,0)).x;
    maxXValueWorld = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, 0, 0)).x;

    playerSize = this.GetComponent<Renderer>().bounds.size;
}

void Move()
{
    if (inputLeft)
    {
        this.transform.position -= new Vector3(speed * Time.deltaTime, 0, 0);
    }
    else
    {
        this.transform.position += new Vector3(speed * Time.deltaTime, 0, 0);
    }
}

void OffscreenCheck()
{

    Vector3 screenPos = Camera.main.WorldToScreenPoint(this.transform.position);
    Vector3 maxWorldXWithPlayerSize = Camera.main.WorldToScreenPoint(new Vector3(maxXValueWorld,0,0) + playerSize/2);
    Vector3 minWorldWithPlayerSize = Camera.main.WorldToScreenPoint(new Vector3(minXValueWorld,0,0) - playerSize/2);

    if (screenPos.x < minWorldWithPlayerSize.x)
    {
        this.transform.position = new Vector3(maxWorldXWithPlayerSize.x, this.transform.position.y, this.transform.position.z);
    }

    if (screenPos.x > maxWorldXWithPlayerSize.x)
    {
        this.transform.position = new Vector3(minWorldWithPlayerSize.x, this.transform.position.y, this.transform.position.z);
    }

}

然后在Update函数中依次调用MoveOffscreenCheck

这段代码的问题是,一旦我的精灵在左边完全离开屏幕,它就会出现在右边的屏幕外,但它不再向左移动了。

相反,它只是传送到屏幕外的左侧或右侧位置。因此,我不再看到它在屏幕上左右移动。

我很确定我的代码逻辑刚刚关闭。有谁知道如何解决这个问题?

谢谢

【问题讨论】:

  • 如果没有a good, minimal, complete code example 清楚地显示行为,很难确定问题可能是什么。但是,您的代码中的一件事对我来说看起来很可疑:new Vector3(maxXValueWorld,0,0) + playerSize/2。我认为new Vector3(maxXValueWorld + playerSize/2,0,0) 会更有意义。按照你的方式,我可以想象在转换到屏幕坐标时会发生一些奇怪的事情,其中​​x 的值不会出现在你认为应该的位置。
  • 感谢您的回复。我已根据您的建议修复了我的代码,但我的对象的行为保持不变。基本上发生的情况是,当对象离开屏幕时,对象的 x 位置变为-19424(至少基于我的屏幕纵横比)。它只是在那些 x 位置之间切换,这是问题所在,因为这些位置已经是屏幕外的位置。我不确定,但我认为问题在于我的 if 语句中的逻辑。
  • 在我的if 块中,当我将maxWorldXWithPlayerSize.x 更改为简单的maxXValueWorld,并将minWorldWithPlayerSize.x 更改为minXValueWorld 时,对象成功出现在屏幕的另一侧。但我想要的是让它从屏幕外出现,这样对象就不会突然出现,它的一半显示(因为精灵的轴心点在中心)

标签: c# unity3d


【解决方案1】:

我认为以下应该可行:

void OffscreenCheck()
{

    Vector3 screenPos = Camera.main.WorldToScreenPoint(this.transform.position);
    Vector3 maxWorldWithPlayerSize = new Vector3(maxXValueWorld + playerSize/2,0,0);
    Vector3 minWorldWithPlayerSize = new Vector3(minXValueWorld - playerSize/2,0,0);
    Vector3 maxScreenWithPlayerSize = Camera.main.WorldToScreenPoint(maxWorldWithPlayerSize);
    Vector3 minScreenWithPlayerSize = Camera.main.WorldToScreenPoint(minWorldWithPlayerSize);

    if (screenPos.x < minScreenWithPlayerSize.x)
    {
        this.transform.position = new Vector3(
            maxWorldWithPlayerSize.x,
            this.transform.position.y,
            this.transform.position.z);
    }

    if (screenPos.x > maxScreenWithPlayerSize.x)
    {
        this.transform.position = new Vector3(
            minWorldWithPlayerSize.x,
            this.transform.position.y,
            this.transform.position.z);
    }

}

以上内容很好地说明了为什么要小心命名变量的方式。当我第一次查看您的代码时,我无法将变量中包含“世界”一词的事实联系起来,它们实际上是在屏幕坐标中。所以我没有注意到位置的错误分配,您使用屏幕坐标在 world 坐标中分配了 X 坐标。

在上面,我将世界和屏幕坐标向量分离为单独的局部变量。屏幕坐标用于屏幕外比较本身,而世界坐标用于将精灵实际移动到您想要的位置。

同样,由于缺少完整的代码示例,我无法实际测试上述内容并验证它是否解决了您的问题。但我认为会的。

【讨论】:

  • 谢谢你,我现在开始工作了!我需要处理我的变量名,因为您发布的上述代码更容易阅读。我想我因为命名混乱而搞砸了我的转换:p。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多