How to aim a particle emitter in correct direction?

Hello,

In my game I have a dragon, from which I want him to shoot fire when he’s attacking an enemy. My problem is that, so far, I havn’t been able to get the correct rotation (or angle) in order for the flame(particle emitter) to hit the enemy directly. The enemies themselves are dynamic and move around, so I’d like to be able to “target” the flame no matter where the enemies are in 3d space (i.e. preferably 360 degrees of rotation freedom).

The very odd thing, is that I already have code for most of my units that rotates them to always be facing their enemy, and it works great! The code works perfectly for my unit gameobjects, but for some reason doesn’t work for my particle emitter game object.

I’ve tried using transform.LookAt as well as the Quaternion.Slerp functions, and they seem to kind of be working, but never are spot on.

Here is my code I’ve been working with which is attached to each dragon game object. I then drag in the “flame” prefab, which is a prefab with a couple children objects related to the particle emitter. The “OuterCore” is the actual game object with the particle emitter on it. The OuterCore is a child of the “Flame” gameobject, which houses things like the OuterCore, a point light source, smoke, etc…:

#pragma strict


public var flamePrefab : GameObject;
public var dragonFlame : GameObject;

//Transform caching
private var thisTransform : Transform;
private var thisRigidbody : Rigidbody;

private var dflametrans : Transform;
private var flamereftrans : Transform;
private var outerCore : Transform;//The actual object with the flame emitter on it

//Using a local enemyTarget variable to refer to the actual one in Attack script
private var enemyTarget : GameObject;
private var enemyT : GameObject; //just needed a variable to reference the cached enemy target


function Awake () {

thisTransform = transform;
thisRigidbody = rigidbody;

}

function Start () {

dragonFlame = Instantiate(flamePrefab, thisTransform.position, flamePrefab.transform.rotation);
//dragonFlameChild = dragonFlame.transform.Find("OuterCore");

dflametrans = dragonFlame.transform;//Caching the flame object's transform position to optimize
outerCore = dragonFlame.transform.Find("OuterCore");


//Caching the transform of the flame reference gameobject (an empty game object on dragons) to position the flame prefab each update
flamereftrans = thisTransform.Find("Flameref").transform;

enemyTarget = GetComponent(Attack).enemyTarget;

}

function Update () {

dflametrans.position = flamereftrans.position;

enemyTarget = GetComponent(Attack).enemyTarget;

			var _lookRotation : Quaternion;
			var _direction : Vector3;
			
			if(enemyTarget != null) {
				_direction = Vector3(enemyTarget.transform.position.x - dflametrans.position.x, enemyTarget.transform.position.y - dflametrans.position.y, enemyTarget.transform.position.z - dflametrans.position.z).normalized;
			}
			
			if ( _direction != Vector3.zero) {
				_lookRotation = Quaternion.LookRotation(_direction);
			}
			//dflametrans.LookAt(Vector3(enemyTarget.transform.position.x, transform.position.y, enemyTarget.transform.position.z));
			if(enemyTarget != null) {
				//outerCore.LookAt(enemyTarget.transform.position);
				//Debug.Log(enemyTarget);
			}
			
			//outerCore.rotation.eulerAngles = Vector3(outerCore.rotation.x, outerCore.rotation.y, outerCore.rotation.z + 180);
			outerCore.rotation.y += 180;
			outerCore.rotation.z += 180;
			
			dflametrans.rotation = Quaternion.Slerp(dflametrans.rotation, _lookRotation, Time.deltaTime);
			outerCore.rotation = Quaternion.Slerp(outerCore.rotation, _lookRotation, Time.deltaTime);


}

So far, the only way to get the flame to point down (the dragons are flying above most enemies), is to add 180 degrees onto the y and z rotation axes.

To be honest, I feel like I’m just taking shots in the dark and coming close sometimes, but never really understanding why the particle emitter refuses to “target” the enemy target exactly.

I solved my problem! And I’m super excited about it :smile:

In my case, I needed my particles to follow my enemy. Well it turns out the rotation of the gameobject the particle emitter is on doesn’t matter that much – i’m not certain, but I think it can be ignored altogether. What matters (in my case at least) is the “World Velocity” on the particle emitter.

Luckily in my case, the x, y, and z distance from my particle emitter game object to my enemy target in the game world (which is why World Velocity matters in this case) corresponds exactly with the World Velocity x,y,z on the particle emitter.

So I set the World Velocity vector3 variables on the emitter to equal the distance my enemy target is from my emitter parent game object, and the particles will emit to that target (with the right amount of “push”).

I guess I was viewing the particle emitter like a gun I could simply aim at my target and the particles would go there. This is not the case. It’s more like wind. You have to push the wind into the position you want it to go :slight_smile:

I posted just in case someone might have my problem.

5 Likes