# Enemy fire does not travel to player using RayCast2D

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?

• The bullet is scripted to always move right regardless of the player's location. Do you mean you want the bullet to follow the player? – TomTsagk Apr 15 '18 at 13:13
• @TomTsagk Yes please, I thought the forward movement will be controlled by enemy rotation? – StuckInPhDNoMore Apr 15 '18 at 17:00

In order to make the bullet follow the player, you can use this:

public class MoveBulletTrail : MonoBehaviour {

public int speed = 200;
Transform target; // What the bullet is following

// Function to call to tell the bullet what to follow
// You can call it like "SetTarget(player.transform);"
public void SetTarget(Transform t) {
target = t;
}

// Update should make the bullet go closer to the target
void Update () {

// Make bullet look at target
transform.LookAt(target.position);

// Make bullet go slightly towards target
// Please note this is in 3D, if you want to use it in 2D
// Change speed to either the X axis or Y axis
transform.Translate(0, 0, speed);

// Optionally check if the bullet reached target
// This example assumes that if bullet is closer than 0.1f units
// it means it reached the target, feel free to lower that number to suit your needs
if ((target.position -transform.position).magnitude < 0.1f) {
// Bullet reached target, destroy it and probably do something else?
Destroy(gameObject);
target.DamagePlayer(); // or whatever
}
}


Basically the idea is that for every bullet, you give them a target to follow. Every frame, the bullet looks towards the target, then moves forward to where it's looking. Once the bullet's distance with the target is small enough, it means they are touching.

Optionally, you can give the player a Collider so that it detects itself when collision happen, but that's another story.

• Thanks but its not working. Leaving the speed at z axix does not show the laser, but as you mentioned, my game is 2D so I changed it to x-axis. The laser just keeps zig-zagging in one place rather than following the player. – StuckInPhDNoMore Apr 15 '18 at 22:19
• @StuckInPhD can you post a gif or small video of the result? – TomTsagk Apr 15 '18 at 22:24