0

I am having trouble getting the enemy's projectile to fly from the enemy to the player's position. When I play the game, the enemy bullet projectiles fly off in one direction on the screen and not toward the player. I think the issue might be in how I am assigning direction to the projectile prefab? Any suggestions would be much appreciated.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyController : MonoBehaviour
{
    public float speed;
    public Rigidbody enemyRb;
    [SerializeField] float rateOfFire;
    private GameObject player;
    public GameObject projectilePrefab;
    float nextFireAllowed;
    public bool canFire;
    Transform enemyMuzzle;


    void Awake()
    {
        enemyRb = GetComponent<Rigidbody>();
        player = GameObject.Find("Player");
        enemyMuzzle = transform.Find("EnemyMuzzle");
    }

    void Update()
    {
        //move enemy rigidbody toward player
        Vector3 lookDirection = (player.transform.position - transform.position).normalized;
        enemyRb.AddForce(lookDirection * speed);

        //overallSpeed
        Vector3 horizontalVelocity = enemyRb.velocity;
        horizontalVelocity = new Vector3(enemyRb.velocity.x, 0, enemyRb.velocity.z);

        // turns enemy to look at player
        transform.LookAt(player.transform);

        //launches projectile toward player
        projectilePrefab.transform.Translate(lookDirection * speed * Time.deltaTime);
        Instantiate(projectilePrefab, transform.position, projectilePrefab.transform.rotation);

    }

    public virtual void Fire()
    {

        canFire = false;
        if (Time.time < nextFireAllowed)
            return;
        nextFireAllowed = Time.time + rateOfFire;

        //instantiate the projectile;

        Instantiate(projectilePrefab, enemyMuzzle.position, enemyMuzzle.rotation);

        canFire = true;
    }
}

1 Answers1

0

It looks like what is actually happening is that you create a bunch of bullets but don't store a reference to them. So each bullets sits in one place while the enemy moves closer to the player ( which might give the appearance that the bullets are moving relative to the enemy. ) I also assume the enemy is moving very fast since it is not scaled by delta time but is being updated every frame.

I think projectilePrefab is just the template object you're spawning, so you probably don't want to move it directly and you certainly don't want to instantiate a new bullet every frame.

If you want to move the object you spawned the least changes ( but still problematic ) from your example code might be:

public class EnemyController : MonoBehaviour
{
    // add a reference
    private GameObject projectileGameObject = null;
    void Update()
    {
        //Update position of spawned projectile rather than the template
        if(projectileGameObject != null ) {
            projectileGameObject.transform.Translate(lookDirection * speed * Time.deltaTime);
        }
       // Be sure to remove this extra instantiate
        //Instantiate(projectilePrefab, transform.position, projectilePrefab.transform.rotation);
    }

    public virtual void Fire()
    {
        //instantiate the projectile
        projectileGameObject = Instantiate(projectilePrefab, enemyMuzzle.position, enemyMuzzle.rotation);
    }
}

Or keep multiple bullets in a list. This implementation has the bug that it will always use the current enemy to player vector as the direction rather than the direction that existed when it was fired.

What you will probably want eventually is that each projectile is has it's own class script to handle projectile logic. All the enemyController class has to do is spawn the projectile and sets it's direction and position on a separate monobehavior that lives on the Projectile objects that handles it's own updates.

Molly J
  • 519
  • 5
  • 15