Hi, I have struggled with this for most of my Saturday and I am reaching out for help. I have a cannon ball that fires and has a ridgedbody 2D component. As the cannon ball flies up and back down to earth in an arc it explodes after a few seconds with Destroy(gameObject,0f);. During the destruction of that object I instantiate a Prefab particle system as shrapnel. I am trying to get the current direction of the cannon ball and translate that to the emission direction of the particle system.
Currently I just have a default number but if the user fires the cannon ball straight up, when the particles are created they explode sideways instead of straight up. I’m getting lost with all the Quaternion, Euler stuff. Below is the Method I am using to destroy the GameObject and Instantiate the particle prefab. If I can figure out how to transfer the GameObject direction to the particles system I can reduce the code and get rid of the left and right cannon ball shots. Currently I have 2 default values 135 and 45 below.
void destroyTheObject(){ // if the timer activates first.
if(destroyCalled){
Vector3 pos = new Vector3(transform.position.x,transform.position.y,-154); // get position of the cannon ball
Quaternion qRotation;
if(isRight){
qRotation = Quaternion.Euler(new Vector3(135, 90, 90)); // if the right side use 135, but this should change to the anlge of the ball.
}
else{
qRotation = Quaternion.Euler(new Vector3(45, 90, 90)); // if the left side use 45, but this should change to the anlge of the ball.
}
GameObject exl = Instantiate (expAnim, transform.position, transform.rotation) as GameObject;// as GameObject; // create the
expScriptController script = exl.GetComponent<expScriptController>();
script.timeLeft = 5.0f;
Instantiate(shrapParticles, pos, qRotation);
Destroy(gameObject,0f);
}
}
There’s various ways this could be done, if it’s all 2d it might be easiest just to get the current velocity of the cannonball’s rigid body (Rigidbody2D.velocity) and apply that to the particles spawn direction. You could normalise the velocity and then find the angle of it using ATan2(vel.y,vel.x) - you could then instantiate your particles with that Z rotation.
Particles can also inherit the parent velocity too, you could attach your particle system at the beginning but only trigger it when the ball is destroyed (so it would already have the parent velocity). Particles can be in ‘local’ space (so they move relative to the parent) or world space (where they move independently).
Hi, thanks for the reply. I tried to implement this this morning and it is still eluding me. I am unclear as to how velocity sets an angle? So just to use the code I think is at work here.
Step1. Get the location point of the current GameObject, z at -154 is needed so it is not hidden
Vector3 pos = new Vector3(transform.position.x,transform.position.y,-154);
Step 3. Apply the velocity of the rigidbody2d to the x which seems to control the direction of the particle system. The y,z 90 make sure it is fading the camera
Using this set up the particle system is firing in the opposite direction, as seen in the photo I attached. The canon ball is in a downward arc and when it triggers the particles they fire backwards.
I finally came upon an answer today that solved this issue, took 2 of my 3 vacation days to solve this problem that I was hoping to make headway on my game