- Home /
Emmiter in time intervals
So i have this object that i want to be emmited in groups of 5 at time intervals of 10 seconds. What would the code be so that I can just drag in the Rigidbody(my object) into the slot to be emmited like this? Thanks in advance!!!
Answer by Aleron · Dec 20, 2011 at 10:09 PM
A very simple script that should roughly do what you want would be:
using UnityEngine;
public class MyEmitter : MonoBehaviour
{
public Rigidbody emittedBody;
void Start()
{
InvokeRepeating( "EmitRigidBody", 10.0f, 10.0f );
}
void Update()
{
if( Input.GetMouseButtonDown(0) )
CancelInvoke();
}
void EmitRigidBody()
{
for( int i = 0; i < 5; ++i )
{
Rigidbody body = Instantiate( emittedBody ) as Rigidbody;
// Do whatever else is wanted with the rigid body
// ...
}
}
}
If you add that script as a component to a GameObject in your scene then you will be able to drag the desired RigidBody onto the 'Emitted Body' parameter of the component. Then as soon as the scene loads it will start emitting the 5 objects every 10 seconds. You can move the InvokeRepeating to whatever piece of code you want to trigger the emitting of the object., I put the CancelInvoke so that clicking the mouse button will stop the objects from being emitted, but you can move it to wherever you want to stop the creationg of the objects from.
this gave me the error:
Assets/Scripts/CSharpScripts/Rigidbody emmiter.cs(1,26): error CS0246: The type or namespace name `$$anonymous$$onoBehaviour' could not be found. Are you missing a using directive or an assembly reference?
Oops, sorry, it should be Rigidbody not RigidBody. I'll edit my response to have the correct info as well.
Sorry, I was rushing when I responded and didn't read the actual error, the script needs using UnityEngine; at the top of it to use the UnityEngine assembly and its classes (like $$anonymous$$onoBehaviour and Rigidbody). I added that the to code snippet above. I also added the casting of the Object to a Rigidbody in the Instantiate so it is the correct type.
Your answer
Follow this Question
Related Questions
Random time activate/deactivate object 1 Answer
TimeScaling & Rigidbody problem 0 Answers
Problem with Object collision 1 Answer
Fade between group of object 1 Answer
Emitting objects at a progressive rate randomly in a 2D game 1 Answer