- Home /
Spawning on Trigger
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
Doesn't look like it posted my answer to your question.
I maybe wrong but I don't think you are saying where you want to instantiate it yet.
I think you want something along the lines of:
var obj = Instantiate(batPrefab,startingPosition.position, Quaternion.identity); obj.rigidbody.addForce(forward*speedToGoForward);
hope that helps
$$anonymous$$ind Regards
$$anonymous$$elissa
Thanks but I'm getting this error with that Field'System.Int32.position' not found.
and here is the code with your revision.
var batPrefab:Transform; // Insert bat prefab var batAmount:int = 20; // Number of bats to be spawned instantly var startingPositionY : int = 25; var speedToGoForward : int = 10; var forward: int = 10 ;
function OnTriggerEnter (Bats : Collider) { Debug.Log("hitTrigger"); for(i=0;i
Answer by daivuk · Mar 29, 2012 at 07:33 PM
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
Answer by daivuk · Mar 29, 2012 at 07:33 PM
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.
I want the bats to move in a forward motion right now when I hit the trigger they are scattrered
I think that should be the bat script to handle this in his Start() function. Apply an initial velocity to them, or update them forward every frame in their Update()
Answer by Melissa_Goddard · Mar 29, 2012 at 07:33 PM
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