Need help instantiating relative to parent object's local position.

I’m trying to write a script that will instantiate an object as you walk through its parents; the end goal if this is so that I can send the player down a long tunnel whilst only having to instantiate a few tunnel segments, saving the polycount. So far I’ve written code that can spawn a cube at global position 0,0,0 once the parent cube is entered by the player. But I’m finding it difficult to get this cube to spawn 2 or 3 units to the side of the parent cube itself. The parameter for the Instantiate method isn’t taking the localVariable code, so I can’t manipulate the child cube’s position with respect to the parent’s local variable. Here is the script. (javascript)

var original : GameObject; // References whatever object I attach (the parent cube).

function OnTriggerEnter (trigger : Collider)
{
if(trigger.gameObject.name == “Player”)
{
var instance : GameObject = Instantiate(original, Vector3(2.0, 0.0, 0.0), transform.rotation);
}
}

It checks if the player is colliding with the parent cube, then Instantiates the object assigned to ‘original’ at position 0,0,0. If I were to add the variable localPosition before the Vector 3 declaration, the compiler would reject it.

I’m sure I’m doing something wrong, probably misunderstanding how localPosition should be used or something, but I’m kind of new to coding. I’d appreciate some help.

I don’t really use UnityScript, so perhaps my syntax is incorrect - but I don’t see why this shouldn’t work.

var instance : GameObject = Instantiate(original, transform.localPosition + Vector3(2.0, 0.0, 0.0), transform.rotation);

Assuming the 2.0 in the x means you want the object to spawn 2 units in front of you…

var instance : GameObject = Instantiate(original, transform.localPosition + (transform.forward * 2), transform.rotation);

Doesn’t seem to work. First example of code gives the error message ‘Method not found: Unity.Transform.localPositionVector3’. No idea why 'transform is error messaged in capitals.

The code KyleStaves provided is correct and does not generate any errors.

–Eric

I am just indulging in some calculated speculation here, but you most likely forgot the ‘+’ operator between ‘localPosition’ and 'Vector3

var instance : GameObject = Instantiate(original, transform.localPosition + Vector3(2.0, 0.0, 0.0), transform.rotation);

Ah thanks very much. What a dumb mistake. Makes sense now.

Well, I guess the gossip section is the scripting section now.