Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Enemies in my first FPS game

Firethrone482Firethrone482 Member
edited May 2020 in Programming

Edit: I have Managed to sort it but thank for trying to help


I'm a relative beginner to unity and am having issues with the enemies in the game i'm making, my code is in no way efficient (mostly a bodge job of tutorials and when I couldn't find a tutorial some coding from me) but it works for the most part. My main issue is that enemies of the same type ( i have two types so far) are not both/all shooting at the same time, what happens is instead only one enemy will fire until it is dead and then the next enemy will fire, I currently have all enemies in the scene (not spawned upon game start) however i have tried with instantiating and it doesn't make a difference. I'm assuming it is due to the sharing scripts but am unsure (also don't know if that was a clear explanation but I hope so)

Comments

  • Did u use a prefab and having the enemy script on each of them? Because if u need to spawn the same gameobject u need to make a prefab.

  • I am using prefabs and am making any changes to the prefab not the instances in the scene view


  • Did the other enemy recognize u as a player? Did the enemy that didnt shoot looking at you?

  • Firethrone482Firethrone482 Member
    edited May 2020

    Yes both enemies follow and look at me but only one (of each type) is able to shoot at a time

    it's an issue that been confusing me for a day or two now


    thx for helping btw

  • WarpWarp Administrator
    edited May 2020

    You said that they shared scripts? Can you elaborate on that.

    Send the script you use for the enemy, and a screenshot of the enemy prefab inspector.

  • Firethrone482Firethrone482 Member
    edited May 2020

    The Gun is where the actual shooting occurs, All enemies share the enemy Controller Script (find player, go to player trigger , shoot function) all works well. The different types of enemy have different gun control scripts that are both basically identical, I can't really remember my logic behind that but i think it was something to do with it wasnt working unless i split them (Inefficiently)

    void Start()

      {

        target = PlayerManager.instance.player.transform;

      }

      // Update is called once per frame

      void Update()

      {

        count = count + 1;

        print(count);

        nextTimeToFire = Time.time / fireRate;

        //if (Time.time > -nextTimeToFire)

      }

      public void Trigger()  //latest Addition , triggered once the enemy has stopped and is looking at the player

      {

        Invoke("Fire", 1.0f);

      }

      public void Fire()

      {

        if(count > fireRate )

        {

          muzzleFlash.Play();

          RaycastHit hit;

          if (Physics.Raycast(CastPoint.transform.position, CastPoint.transform.forward, out hit, range))

          {

            print("really works");

            Debug.Log(hit.transform.name + "(enemy)");

            PlayerHealth playerHealth = hit.transform.GetComponent<PlayerHealth>();

            if (playerHealth != null)

            {

              print("really reeeeeeeee works");

              playerHealth.TakeDamage(damage);

            }

            if (hit.rigidbody != null)

            {

              hit.rigidbody.AddForce(-hit.normal * impactForce);

            }

            GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));

            Destroy(impactGO, 0.21f);

          }

          count = 0;

        }

      }

    edit: All variable declared at the top I didn't copy them for some reason

    note: count 2 is redundant as is bullets per second i had forgotten to remove them before sending the screenshot, others may also be redundant but i haven't noticed.

  • Can u show me the enemy controller script?

  • public class EnemyController : MonoBehaviour

    {

      public float lookRadius = 10f;

      Transform target;

      NavMeshAgent agent;

      public int type;

      bool gamePaused = false;


      // Start is called before the first frame update

      void Start()

      {

        target = PlayerManager.instance.player.transform;

        agent = GetComponent<NavMeshAgent>();

      }

      // Update is called once per frame

      void Update()

      {

        float distance = Vector3.Distance(target.position, transform.position);


        if(distance<= lookRadius)

        {

          //EnemyGun.Shoot();

          agent.SetDestination(target.position);


          if(distance <= agent.stoppingDistance)

          {

            FaceTarget();

          }

        }

      }

      void FaceTarget()

      {

        Vector3 direction = (target.position - transform.position).normalized;

        Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));

        transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);

        if (type == 1)

        {

          FindObjectOfType<EnemyGun>().Trigger();

          print("fire");

           

        }

        else if (type == 2)

        {

          

          FindObjectOfType<EnemyGun2>().Trigger();

          print("fire");

           


        }

      }

      private void OnDrawGizmosSelected()

      {

        Gizmos.color = Color.red;

        Gizmos.DrawWireSphere(transform.position, lookRadius);

      }

    }


    that's the enemy controller

  • Firethrone482Firethrone482 Member
    edited May 2020

    Edit I was wrong with what i put on this comment

  • I think i have fixed it, I combined the scripts together and it seems to work

Sign In or Register to comment.