0
\$\begingroup\$

I am designing an FPS game and scripted this shooting enemy that is supposed to shoot at the player if he is in the trigger zone once every second, but it just rapid fires and InvokeRepeating doesn't seem to do anything. No errors or anything. I am new to programming so it is most likely a rookie mistake. So here is the code

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

public class EnemyAI2 : MonoBehaviour
{
    public float fireRate = 10f;
    public float CanShoot;
    public float forceadded;
    public Transform BulletSpawn;
    public Transform PlayerTransform;
    GameObject player;
    NavMeshAgent enemy;
    public GameObject bulletclone;

    // Use this for initialization
    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player");
        enemy = GetComponent<NavMeshAgent>();
        {
            InvokeRepeating("OnTriggerStay", 1.0f, 1.0f);
        }
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 direction = PlayerTransform.position - this.transform.position;
        enemy.destination = player.transform.position;

    }

    void OnTriggerStay(Collider other)
    {
        if(other.gameObject.tag == ("Player")) 
        {
            gameObject.GetComponent<NavMeshAgent>().enabled = false;
            {
                Vector3 direction = PlayerTransform.position - this.transform.position;
                {
                    var Bullet = (GameObject)Instantiate(bulletclone, BulletSpawn.position, BulletSpawn.rotation);
                    Bullet.GetComponent<Rigidbody>().velocity = Bullet.transform.forward * forceadded;
                    Destroy(Bullet, 1.0f);

                }

            }
        }
        else 
        {
            gameObject.GetComponent<NavMeshAgent>().enabled = true;
            enemy.destination = player.transform.position;
            Vector3 direction = PlayerTransform.position - this.transform.position;
        }
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ It doesn't work because that method requires a Collider parameter which you aren't passing (and, in fact, do not have). Tldr, that's not how OnTriggerStay works, its called automatically for you as needed. \$\endgroup\$ Oct 22, 2018 at 16:30

1 Answer 1

0
\$\begingroup\$

I think you're trying to do a lot of treatments in the OnTriggerStay function.

You should try to create a variable that checks if the player is on sight or not, something like this:

void Update()
{
  if (playerOnSight == true && Time.time > lastTimeFired)
  {
    gameObject.GetComponent<NavMeshAgent>().enabled = false;
    var Bullet = (GameObject)Instantiate(bulletclone, BulletSpawn.position, BulletSpawn.rotation);
    Bullet.GetComponent<Rigidbody>().velocity = Bullet.transform.forward * forceadded;
    Destroy(Bullet, 1.0f);
    lastTimeFired = Time.time + 1.0f;
  }
if(playerOnSight == false)
{
          gameObject.GetComponent<NavMeshAgent>().enabled = true;
        enemy.destination = player.transform.position;
        Vector3 direction = PlayerTransform.position - this.transform.position;
}
}
void OnTriggerStay(collider other)
{
  if (other.gameobject.tag == "Player")
    {
      playerOnSight = true;
    }
}
void OnTriggerExit(collider other)
{
  if (other.gameobject.tag == "Player")
    {
      playerOnSight = false;
    }
}

Note that I haven't tested this code.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you. This seems like it should work. Will definitely mark correct and upvote if it works. (Can't check till tomorrow) \$\endgroup\$ Oct 22, 2018 at 1:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .