- Home /
Initiating several events on collision
Hey everyone. I'm trying to get a series of cannons on a ship to fire at a set location by the collision with a collider trigger.
I have an explosion prefab and a particle system for the smoke. They need to activate at the same time and destroy shortly thereafter.
I have created a box collider tagged as a trigger. My problem is cross-referencing a variable. I have a script attached to the collider, and a script attached to the particlesystems (I have 3 particlesystems as children under one parent gameobject).
My code so far:
// This code is on the trigger collider
using UnityEngine;
using System.Collections;
public class TriggerEvent : MonoBehaviour
{
public bool Col;
void OnTriggerEnter (Collider other)
{
Debug.Log ("Trigger enabled");
Col = true;
}
}
So I'm trying to define a bool variable (Col) that can carry over to a script on all objects that need to do something once the trigger is hit. This will also be used on an object that will instantiate the explosion prefab (for when the cannons fire), as well as playing an animation on the ship itself (swaying from the recoil)
// This code is on each ParticleSystem
using UnityEngine;
using System.Collections;
public class EnableEmission : MonoBehaviour {
// Use this for initialization
void Start () {
GetComponent
}
// Update is called once per frame
void Update () {
if (col == true) {
particleSystem.enableEmission == true;
}
else {
particleSystem.enableEmission == false;
}
}
}
This is my first attempt at scripting so I am sorry if this is really too easy. I have read about GetComponent and GameObject.Find but I am at a loss at how to use them for my project and purpose, I have the same problem with most of the codesamples I find on the net. So until I have expanded my basic knowledge I hope someone can help me with this. Thank you!
Edit: I thought it was worth noting that this is not a game per say. This is more of an demo. All the player can do is look around. The movement of the ship, when the cannons fire etc is all predefined.
I have found code samples to invoke an event after a set amount of time. But I need the event to trigger at a very precise time and position, so I$$anonymous$$O it would be better to place a trigger object or instantiate my prefab when the ships coordinates are equal to a set number (since I know these), rather than ti$$anonymous$$g it precisely enough.
I am however, completely lost on how to do this. This is my first time scripting, and the samples I find lead me down a dead end since I don't know enough to fill out the blanks and adapt it to my project.
So for now I am reading basic scripting tutorials to try and gain a little knowledge. But if anyone knows hows to trigger the creation of a prefab at a specific position inside a gameObject from a collision or set of coordinates, that would be just what I need.
Please update this question to reflect that you want this based on location, not on time, as that is a bit misleading.
Once the question has been updated, I'll be more than happy to push it through. (Although I'd still like to see some code and some more detail about your question).
$$anonymous$$udos for reading tutorials and learning basic scripting in the mean time, btw :)
Answer by Itaros · Oct 28, 2014 at 03:34 AM
It is simple:
On the code on the trigger collider:
public EnableEmission[] trigerable;
void OnTriggerEnter (Collider other){
foreach(EnableEmission ee in trigerable){
ee.MakeActive();
}
}
Fill the array in inspector.
In EnableEmission scripts add:
public void MakeActive(){
//Event code. Activation or whatever
}
P.S.UnityAnswers code editor is horrible
Btw, it can be achieved in reversed way using .net events. For more information check this: http://msdn.microsoft.com/en-us/library/awbftdfh.aspx
Thank you Itaros !
(I deleted the answer and posted it here as a comment. I'm reading the page you linked me to now, and i placed the code in the class scope. No errors now. Thanks :) )
Your answer
Follow this Question
Related Questions
Shuriken OnParticledCollision 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
problem with collision detection 0 Answers
Collision based scoring system 1 Answer
Plane flyby triggering/help 1 Answer