【问题标题】:this.transform.position + new Vector3(0,y,0) changes the z axis of the game objectthis.transform.position + new Vector3(0,y,0) 改变游戏对象的z轴
【发布时间】:2017-05-09 03:39:06
【问题描述】:

我在每 1 秒间隔后实例化一个特定的预制件。

float y = Random.Range(-0.5f, 1f);
GameObject newObject = Instantiate (this.prefabToSpawn, this.transform.position + new Vector3(0,y,0),Quaternion.identity);

每次实例化时,我只想改变游戏对象沿 y 轴的位置,但是如果我添加上面的 sn-p ,新的游戏对象会沿 z 轴落后很多(即使有虽然 y 轴也有随机变化,这似乎工作正常,它只需要保留它的 z 位置),因此在场景中不再可见。我哪里错了?

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您正在将新的Vector3 添加到this.transform.position。问题是this.transform.position 会改变你的x、y 和z 轴。如果您不希望发生这种情况,则必须创建一个临时的 Vector3 变量,您将存储 transform.position + new Vector3(0, y, 0); 的结果,然后您只需修改 Z 轴并将其设置为 0 或您的值希望它保留。

    float YOUR_DEFAULT_Z = 0f;
    float y = UnityEngine.Random.Range(-0.5f, 1f);
    Vector3 tempPos = transform.position + new Vector3(0, y, 0);
    //Overide the Z axis 
    tempPos.z = YOUR_DEFAULT_Z;
    
    GameObject newObject = Instantiate(prefabToSpawn, tempPos, Quaternion.identity);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-21
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      • 1970-01-01
      • 2022-10-13
      • 1970-01-01
      相关资源
      最近更新 更多