Enemies are using raycast to fire and detect player. The bullet has its own script that makes it move forward after instantiating it, but the problem is that it always moves in the right direction and does not take into account the rotation of the enemy.
Here is my enemy fire script
public class EnemyFire : MonoBehaviour {
public GameObject ustaazPlayer;
public GameObject enemyLaser;
float timeToSpawnEffect = 0;
public float effectSpawnRate = 4;
public int damage;
// Use this for initialization
void Start () {
InvokeRepeating("EnemyShoot", 0.00001f, effectSpawnRate);
}
void EnemyShoot()
{
Vector2 playerPosition = new Vector2(ustaazPlayer.transform.position.x, ustaazPlayer.transform.position.y); //position of player
Vector2 bossPosition = new Vector2(transform.position.x, transform.position.y); //enemy location
RaycastHit2D hit = Physics2D.Raycast(bossPosition, playerPosition - bossPosition, 100);
//if (Time.time >= timeToSpawnEffect)
//{
Instantiate(enemyLaser, transform.position, transform.rotation);
AudioManager.instance.PlaySound("enemyLaser");
// timeToSpawnEffect = Time.time + 1 / effectSpawnRate;
//}
Debug.DrawLine(bossPosition, (playerPosition - bossPosition) * 100, Color.cyan);
if (hit.collider != null)
{
Debug.DrawLine(bossPosition, hit.point, Color.red);
//Debug.Log("We hit " + hit.collider.name + " and did " + damage + " damage.");
Player player = hit.collider.GetComponent<Player>();
if (player != null)
{
player.DamagePlayer(damage);
}
}
}
}
and here is the script on the laser object
public class MoveBulletTrail : MonoBehaviour {
public int speed = 200;
// Update is called once per frame
void Update () {
transform.Translate(Vector3.right * Time.deltaTime * speed); //move it forward in the direction it was spawned
Destroy(gameObject, 1); //destroy the trail after 1 second so that inspector does not get bogged down in them - so much easier than having a shredder!
}
}
How would I make it shoot towards the player?