So I have been 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 rapidfires and invokerepeating doesnt 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. The script works and the enemy behaves as intended, but it rapidfires?
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", 1f, 2f);
}
// 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;
}
}
}
,So I have been 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 rapidfires and invokerepeating doesnt 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 (the brackets are in the right place, it just doesn’t put the entire code as code format for some reason)
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();
{
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().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;
}
}
}