【问题标题】:Unity 3d how to instantiate object in a certian positionUnity 3d如何在特定位置实例化对象
【发布时间】:2014-10-18 01:22:59
【问题描述】:

使用 UnityEngine; 使用 System.Collections;

公共类 playSound : MonoBehaviour {

private bool destructionHasBegun = false;
public Transform BlueKey;


private void OnTriggerEnter()
{
    audio.Play ();
    destructionHasBegun = true;
}

private void Update()
{
    if(destructionHasBegun)
    {
        DestroyWhenSoundComplete();
    }
}

private void DestroyWhenSoundComplete()
{
    if(!audio.isPlaying)
    {
        Destroy(gameObject);
        GameObject textObject = (GameObject)Instantiate(Resources.Load("BlueKey")); 





    }
}

}

我试图在特定位置实例化 bluekey 预制件,我该怎么做?提前致谢

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    根据Instantiate documentation,您可以传入 Vector3 世界空间位置作为第二个参数。

    您还需要传入一个四元数作为其初始旋转的第三个参数; Quaternion.identity 相当于没有旋转。

    Vector3 newPosition = new Vector3(0, 0, 0);
    Quaternion newRotation = Quaternion.identity;
    GameObject textObject = (GameObject)Instantiate(Resources.Load("BlueKey"), newPosition, newRotation);
    

    【讨论】:

    • 我收到此错误“UnityEngine.Vector3”是“类型”,但用作“变量”(CS0118)(Assembly-CSharp)
    • @user3482470 啊,抱歉,忘记在 Vector3 构造函数前面写 new。请查看更新后的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多