0
votes

Firstly, this is the script I'm using:

var object: GameObject;
private var obj: GameObject;

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

Now, with this script, when I enter the Trigger, the Prefab is being instantiated in front of my initial Prefab, just like I wanted it...but when I move to my instantiated Prefab, when I enter the Trigger, the prefab is not being cloned in front but at the same position like the last one.

My game is an endless runner, so I need the spawned prefab to always be one tile further of my last prefab. How can I do this?!

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

2

2 Answers

1
votes
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);
0
votes

The code you posted instantiates the prefab always at position (0,0,0). You can simply substitute that vector with the actual world space position where you want to spawn the prefab. For example, in front of the player of a few units:

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