Hey everyone. I've run into an issue with using "Turret-type" enemies in a simple 2D game, programmed to fire a projectile based on the direction and rotation it's currently in. The problem is that when I have two facing different directions, their projectiles get 'confused' as to which way to go, is the best way to put it. I have tested it with just one enemy and it works fine. Is there a better way to make sure that the bullet 'knows' specifically which direction it was fired from? Thanks in advance. For reference, here's the code for the bullet in question I'm using:
public class EBullet1Behavior : MonoBehaviour {
private Rigidbody2D bulletRigi;
public Playermovement playerMovement;
public EnemyType2Properties enemyType2;
public FindVector findVector;
public Transform bulletPoint;
public float speed;
public float destroyTime;
// Use this for initialization
void Start () {
playerMovement = FindObjectOfType ();
enemyType2 = FindObjectOfType ();
findVector = FindObjectOfType();
bulletRigi = GetComponentInParent ();
bulletRigi.velocity = new Vector2 (findVector.x * speed, findVector.y * speed);
transform.right = new Vector3(findVector.x, findVector.y, 0);
}
// Update is called once per frame
void Update () {
Destroy (gameObject, destroyTime);
}
}
While the firing bit is fairly simple:
IEnumerator Firing() {
if (stunned == false && canFire == true)
{
Instantiate(EnemyBullet, bulletPoint.position, bulletPoint.rotation);
yield return new WaitForSeconds(fireSpeed);
Debug.Log("Fire!");
canFire = true;
// Firing capability is turned 'off' elsewhere in the script
Update();
}
}
↧