Unity 3D:实例化预制OnTriggerEnter

时间:2014-01-02 21:49:20

标签: unity3d instantiation

首先,这是我正在使用的脚本:

var object: GameObject;
private var obj: GameObject;

function OnTriggerEnter(other: Collider)
{
if (other.tag == "Player"){                             
        obj = Instantiate(object, Vector3(0, 0, 0), transform.rotation);    
                          }
}

现在,有了这个脚本,当我进入Trigger时,Prefab正在我的初始Prefab前面实例化,就像我想要的那样......但当我移动到我的实例化Prefab时,当我进入Trigger时,预制件不是在前面克隆,而是在与前一个相同的位置。

我的游戏是一个无尽的跑步者,所以我需要生成的预制件始终是我最后一个预制件的一块瓷砖。我怎么能这样做?!

Here's a sketch of the situation, if I'm not being quite clear with you guys

2 个答案:

答案 0 :(得分:1)

var spawnDistInFrontOfPlayer : float = 2f;
//change this to your axis direction (direction the character will run)
var spawnAxis : Vector3 = Vector3.right;

obj = Instantiate(object, other.transform.position + (spawnAxis * spawnDistInFrontOfPlayer), transform.rotation);

答案 1 :(得分:0)

您发布的代码实例化预制件始终位于位置(0,0,0)。您可以简单地将该矢量替换为您想要生成预制件的实际世界空间位置。例如,在几个单位的玩家面前:

spawnDistInFrontOfPlayer = 2f;
obj = Instantiate(object, other.transform.position + other.transform.forward * spawnDistInFrontOfPlayer, transform.rotation);