Hi, I’ve just started using Unity and this is my first question. I searched for similar question using keywords like “true”, “false”, and “collision”, but none of the treads answered my question, so please allow me to ask here.
I am trying to write a JavaScript code, attached to a Sphere, that besides other processes, recognizes when the sphere hits a Cube. When it does, the cubes tagged “Player” emits particles. If it is already emitting, it pauses, and if it is paused, it emits again. The script is as follows (I believe “function OnCollisionEnter(collision:Collision)” is the only part relevant, but I will post the whole script just to be sure):
#pragma strict
function FixedUpdate (){
var x:float = Input.GetAxis("Horizontal");
var y:float = Input.GetAxis("Vertical");
rigidbody.AddForce(x, 0, y);
}
function Start(){
var Cube:GameObject = gameObject.Find('Cube');
var particle:ParticleSystem = Cube.gameObject.GetComponent('ParticleSystem');
particle.Play();
}
function OnCollisionEnter (collision:Collision){
if (collision.gameObject.tag == "Player"){
var halo:Behaviour = collision.gameObject.GetComponent('Halo');
halo.enabled = true;
var particle:ParticleSystem = collision.gameObject.GetComponent('ParticleSystem');
if(particle.isPaused == false){particle.Pause();}
if(particle.isPaused == true){particle.Play();}
}
}
function OnCollisionExit (collision:Collision){
if (collision.gameObject.tag == "Player"){
var halo:Behaviour = collision.gameObject.GetComponent('Halo');
halo.enabled = false;
}
}
I assume since Unity reads the script from top to bottom, after pausing the particle emission, isPaused becomes true, triggering particle.Play(); which means the particle emission won’t pause at all.
How can I make unity recognize whether or not the particle emission is paused at collision time, and act accordingly? Or any other solution is fine as well.
Thank you for your time.