system
March 29, 2012, 6:50pm
1
Hey Everyone
What I need is help to spawn a gameobject once a trigger is hit and I want to spawn in a forward motion.
Here the code that I attached to an empty game object.
var batPrefab:Transform; // Insert bat prefab
var batAmount:int = 20; // Number of bats to be spawned instantly
function OnTriggerEnter (Bats : Collider) {
for(i=0;i<batAmount;i++)
{
var obj : Transform = Instantiate(batPrefab);
obj.parent = transform;
system
March 29, 2012, 7:33pm
2
Have you tried OnTriggerStay(Bats:Collider)
This one will be called every frame.
If you want it to be called only once, you could add a bool or something.
ref: http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnTriggerStay.html
system
March 29, 2012, 7:33pm
3
I am not sure what your question is?
That code to me look like it should work. But i would personally use GameObject for the batPrefab instead of a Transform.
system
March 29, 2012, 7:33pm
4
I may be wrong, but you aren’t telling it where to instantiate…
would it not be:
var obj = Instantiate(batPrefab, startingPosition,Quaternion.identity);
obj.rigidbody.addForce(forward*speedToGoForward);
//Where starting position is the position of the trigger and speedToGoForward is how fast the initial value is when it is instantiated
Hope it helps
Melissa