C# 根据主摄像头在unity3d中的当前位置更改其位置

C# 根据主摄像头在unity3d中的当前位置更改其位置,c#,unity3d,C#,Unity3d,我正在创建一个2d游戏,其中我的相机位置y轴必须根据其当前y位置进行更改/减小 public void continuebut() { campos = 0; campos = transform.position.y; if (campos <0) { campos += 4;//negative y position so the result will decrease the y position transfor

我正在创建一个2d游戏,其中我的相机位置y轴必须根据其当前y位置进行更改/减小

public void continuebut()
{
    campos = 0;
    campos = transform.position.y;

    if (campos <0)
    {

        campos += 4;//negative y position so the result will decrease the y position
        transform.Translate(0, campos, 0);
    }
    else 
    {

        campos -= 4;//positive y position so the result will decrease the y position
        transform.Translate(0, campos, 0);
    }

}
public void continuebut()
{
坎波斯=0;
campos=变换位置y;

如果(campos我可能会离开,但我想你是在谈论这个:

public void continuebut()
{
    campos = 0;
    campos = transform.position.y;


    if (campos >0)
    {
        // If camera position is positive
        // Decrease camera y position
        campos -= 4;//positive y position so the result will decrease the y position
        //Translate camera position
        transform.Translate(0, campos, 0);
    }
    else 
    {
        // If camera position if negative or zero
        // Increase camera y position
        campos += 4;//negative y position so the result will increase the y position
        //Translate camera position
        transform.Translate(0, campos, 0);
    }

}

我可能走了,但我想你说的是:

public void continuebut()
{
    campos = 0;
    campos = transform.position.y;


    if (campos >0)
    {
        // If camera position is positive
        // Decrease camera y position
        campos -= 4;//positive y position so the result will decrease the y position
        //Translate camera position
        transform.Translate(0, campos, 0);
    }
    else 
    {
        // If camera position if negative or zero
        // Increase camera y position
        campos += 4;//negative y position so the result will increase the y position
        //Translate camera position
        transform.Translate(0, campos, 0);
    }

}
首先,

campos=0;
没有意义,因为它后面紧跟着:

campos=transform.position.x;
事实上,编译器可能会完全跳过它,而这一行不会出现在最终代码中

关于代码的其余部分,您需要重新思考您实际对应用程序发出的指令。 首先确定y轴的位置,让我们假设

transform.position.y=5.0f;
所以

你的应用确实减少了campos

campos-=4;
所以坎波斯变成了5-4=1.0f 现在,你知道了

campos.Traslate(0,1.0f,0);
所以你基本上是在向上移动你的物体。 你可能想做的是

transform.Translate(0,-4.0f,0);

然而,考虑到当它接近0时,你可能最终得到一个振荡对象,每帧4个单元上下摆动。

首先,

campos=0;
没有意义,因为它后面紧跟着:

campos=transform.position.x;
事实上,编译器可能会完全跳过它,而这一行不会出现在最终代码中

关于代码的其余部分,您需要重新思考您实际对应用程序发出的指令。 首先确定y轴的位置,让我们假设

transform.position.y=5.0f;
所以

你的应用确实减少了campos

campos-=4;
所以坎波斯变成了5-4=1.0f 现在,你知道了

campos.Traslate(0,1.0f,0);
所以你基本上是在向上移动你的物体。 你可能想做的是

transform.Translate(0,-4.0f,0);

然而,考虑到当它接近0时,你可能会得到一个振荡对象,每帧4个单元上下摆动。

实际上,在该脚本中,条件是0。你建议我已经做了但是没有工作。也许你需要替换变换。转换。位置?或者指定空间关系。否决参数为Space.Self。您得到了什么样的结果?Origin可能在其他任何地方。实际上,在该脚本中,条件为0。您建议我已经做了但不起作用的内容可能需要替换transform。Translate为transform.position?或指定Space relativeTo参数为Space.Self。您得到了什么样的结果?Origin可能在其他任何地方。