Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使我的精灵对象离开屏幕到左侧并显示在右侧?_C#_Unity3d - Fatal编程技术网

C# 如何使我的精灵对象离开屏幕到左侧并显示在右侧?

C# 如何使我的精灵对象离开屏幕到左侧并显示在右侧?,c#,unity3d,C#,Unity3d,在Unity中,我试图让我的精灵离开屏幕到左边,让它出现在右边,反之亦然。顺便说一句,我让我的精灵不断地向左或向右移动(取决于用户的输入) 我考虑到精灵需要完全脱离屏幕,然后才能显示在右侧 以下是我当前的代码: void Start () { minXValueWorld = Camera.main.ScreenToWorldPoint(new Vector3(0,0,0)).x; maxXValueWorld = Camera.main.ScreenToWorldPoint(

在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);
    }

}
void Start()
{
minXValueWorld=Camera.main.ScreenToWorldPoint(新矢量3(0,0,0)).x;
maxXValueWorld=Camera.main.ScreenToWorldPoint(新矢量3(Screen.width,0,0)).x;
playerSize=this.GetComponent().bounds.size;
}
无效移动()
{
如果(输入左)
{
this.transform.position-=新矢量3(速度*时间增量,0,0);
}
其他的
{
this.transform.position+=新矢量3(速度*时间增量,0,0);
}
}
无效偏移检查()
{
Vector3 screenPos=Camera.main.WorldToScreenPoint(this.transform.position);
Vector3 maxWorldXWithPlayerSize=Camera.main.WorldToScreenPoint(新Vector3(maxXValueWorld,0,0)+playerSize/2);
Vector3 minWorldWithPlayerSize=Camera.main.WorldToScreenPoint(新的Vector3(minXValueWorld,0,0)-playerSize/2);
如果(屏幕位置xmaxWorldXWithPlayerSize.x)
{
this.transform.position=新矢量3(minWorldWithPlayerSize.x,this.transform.position.y,this.transform.position.z);
}
}
然后在
Update
函数中依次调用
Move
OffscreenCheck

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

相反,它只是传送到屏幕左侧或右侧的位置。因为这个原因,我看不到它在屏幕上向左或向右移动

我很确定我的代码逻辑是错的。有人知道如何解决这个问题吗


谢谢

我认为以下方法应该有效:

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);
    }

}
void OffscreenCheck()
{
Vector3 screenPos=Camera.main.WorldToScreenPoint(this.transform.position);
Vector3 maxWorldWithPlayerSize=新Vector3(maxXValueWorld+playerSize/2,0,0);
Vector3 minWorldWithPlayerSize=新的Vector3(minXValueWorld-playerSize/2,0,0);
Vector3 maxScreenWithPlayerSize=Camera.main.WorldToScreenPoint(maxWorldWithPlayerSize);
Vector3 minScreenWithPlayerSize=Camera.main.WorldToScreenPoint(minWorldWithPlayerSize);
如果(屏幕位置xmaxScreenWithPlayerSize.x)
{
this.transform.position=新矢量3(
minWorldWithPlayerSize.x,
这个。变换。位置。y,
这个函数是.transform.position.z);
}
}
上面是一个很好的例子,说明了为什么要小心如何命名变量。当我第一次看你的代码时,我没有连接到这样一个事实,即虽然变量中有“World”一词,但它们实际上在屏幕坐标中。所以我没有注意到错误的位置分配,你用屏幕坐标来分配世界坐标中的X坐标

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


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

如果没有这一点,就很难确定问题出在哪里。然而,在我看来,您的代码中有一点是可疑的:
newvector3(maxvalueworld,0,0)+playerSize/2
。我认为这将更有意义,因为新的Vector3(maxXValueWorld+playerSize/2,0,0)。按照您的方式,我可以想象一些奇怪的情况正在转化为屏幕坐标,
x
值没有达到您认为应该达到的位置。谢谢您的回复。我已经按照您的建议修复了代码,但对象的行为保持不变。基本上,当对象离开屏幕时,对象的x位置变成
-19
424
(至少基于我的屏幕纵横比)。它只是在x位置之间切换,这是问题所在,因为这些位置已经是屏幕外的位置了。我不确定,但我认为问题在于我的
if
语句中的逻辑。在
if
块中,当我将
maxWorldXWithPlayerSize.x
更改为简单的
maxXValueWorld
,将
minWorldWithPlayerSize.x
更改为
minXValueWorld
,对象成功显示在屏幕的另一侧。但是我想要的是让它从屏幕外显示,这样对象就不会因为它的半部分显示而突然出现(因为精灵的轴心点在中心)谢谢,我现在让它工作了!我需要处理我的变量名,因为上面的代码更容易阅读。我想我搞砸了我的转换,因为命名混乱:p。