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.