Tijs
December 20, 2012, 5:41pm
1
Dear community,
Im working on a vertical 2D platformer , and i am trying to use if statements to trigger certain events in my game.
for example when the player hits a collider, i want to spawn a prefab(comet) with a constant force component added to it.
Ive tried all kinds of code, but i cant seem to make it work.
it is for a school project and would realy appreciatie any kind of help or advise.
Make sure your prefab has a rigidbody attached to it, and try something like this (note that code hasn’t been tested)
void OnTriggerEnter(Collider col) {
if (col.gameObject.tag == "MyTag") {
GameObject ref = Instantiate(myObj, myPosition, myRotation) as GameObject;
ref.GetComponent<Rigidbody>().AddForce(new Vector3(myForceX, myForceY, myForceZ) * mySpeed)
}
}
Tijs
December 27, 2012, 10:25pm
3
To make this work i used the Enable/Disable Component on trigger to call it in. and destroy(object,5) to make it disappear. But it seems i cant destroy a rigidbody that way.
I found this video explaining it pretty good, also tells you how to spawn something that falls down, a rigidbody: Unity 5 - How To Spawn Objects Using a Trigger - YouTube