Hey,
I have a script that changes the forward direction of a bullet spawner object depending on whether the Player has locked onto an enemy. The conditions are simple;
- If an enemy IS targeted - set forward direction of spawner towards enemy’s position.
- If an enemy IS NOT targeted face forward direction again - i.e. shoot ahead as normal.
The issue I’m having is that when the Player rotates away from his starting position (which points along the World Vector3.forward to begin with), the spawner continues to fire in that same direction.
If anyone can lend a hand that would be great. I seem to recall something about a ‘Relative forward’ calculation to find the forward direction of local objects (BTW, it’s attached to the Player, as expected).
The relevant code is below - Thanks
// Access the ClosestEnemy script and check whether the we have an enemy targeted.
if (ClosestEnemyScript_ref.EnemyTargetted)
{
// Debug.Log("ENEMY TARGETTED");
// Calculate the relative position of the Player to the Enemy and store in bulletDirection (direction)
bulletDirectionR = (ClosestEnemyScript_ref.EnemyPosition - BulletSpawnerR_obj.transform.position).normalized;
}
// Otherwise, no enemy is target and so we just shoot forward
else
{
// Debug.Log("NO ENEMY TARGETTED");
// Hmmmm ??
bulletDirectionR = Vector3.forward;
}
BulletSpawnerR_obj.forward = bulletDirectionR;
}