AI Machien gun Firing problem

Hi have just do codes for several Hours …then get a problems , here it is :hushed:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//using System.Linq;

public class UjiCobaNembak : MonoBehaviour {
	public NavMeshAgent agent ;
	public Transform target ; 
	public Transform startRayCast;
	public Vector3 placeTarget = Vector3.zero;
	public List<GameObject> targets = new List<GameObject>();
	public FSMState state;
	public float speed = 10.0f;
	public float dist = 100.0f;
	public float shootDis = 100.0f;
	public float rateFire = 3.0f;
	public float nextFire = 0.0f;
	// Use this for initialization
	void Start()
	{
		//agent.enabled=false;
		//posisiRayCast = transform.Find("Musuh/ppsh41/Tempat RayCast");
		//Debug.Log(tempatRayCast.name);
		agent=GetComponent<NavMeshAgent>();
		state=FSMState.Idle;
		CollectTarget();
		target = targets[Random.Range(0,targets.Count)].transform;
		
	}
	void Awake () {
		placeTarget = transform.position + Random.insideUnitSphere * 10;
		
	}
	
	// Update is called once per frame
	void Update() {
	if((transform.position - target.position).sqrMagnitude <= jarak )
		{
			state = FSMState.Berjalan;
		}
	if(state == FSMState.Walk)
		{
			StartCoroutine("Walk");
		}
	if(state==FSMState.Idle)
		{
			agent.enabled = false;
			agent.updatePosition=false;
			agent.updateRotation=false;
		}
	if(state == FSMState.Quiet)
		{
			transform.LookAt(target.position);
			agent.enabled =false;
			agent.updatePosition=false;
			agent.updateRotation=false;	
			StartCoroutine(Shoot());
			//InvokeRepeating("InvokeTembakan",0.0f,0.01f);
			//nextFire = Time.time + rateFire;
			//Debug.Log(nextFire);
			
		}
	//Debug.Log(state);
	} 
	
	void CollectTarget()
	{
		if(GameObject.FindGameObjectsWithTag("Player") != null)
		{
			foreach(var x in GameObject.FindGameObjectsWithTag("Player"))
			{
				targets.Add(x);
			}
		}
	}
	public IEnumerator Mlaku()
	{
		agent.enabled=true;
		agent.speed = speed;
		agent.SetDestination(placeTarget);
		while(!Mathf.Approximately(0,agent.remainingDistance))
		{
			agent.updatePosition=true;
			agent.updateRotation=true;
			yield return new WaitForEndOfFrame();
		}
		state = FSMState.Diam;
		yield break;
	} 
	
	void InvokeTembakan() /// i do not use this because very Expensive if it was combined with rayCasting :razz:
	{
		RaycastHit hit;
		if(Physics.Raycast(tempatRayCast.position,tempatRayCast.forward,out hit,jarakTembakan))
		{
			if(hit.transform.CompareTag("Player"))
			{
				Debug.Log("Menembak");
			}
		}
		//nextFire = Time.time + rateFire;
		Debug.Log("Gak kena");
	}
	public IEnumerator Shoot() // Probably here is the Bug 
	{
		Debug.Log(state);
		RaycastHit hit;
		int probability ;
		state= FSMState.Diam;
		while(state ==FSMState.Diam)
		{
			probability = (int) Random.value;
			if(probability == 0)
			{
				yield return new WaitForSeconds(Random.value);
			}
			else if(probability == 1 )
			{
				if(Physics.Raycast(tempatRayCast.position,tempatRayCast.forward,out hit,jarakTembakan))
				{
					if(hit.transform.CompareTag("Player"))
					{
						Debug.Log(hit.transform.name);
					}
					Debug.Log("Dont Hit Me");
				}
				yield return new WaitForEndOfFrame();
			}
			//yield return null;
		}
	}
}
public enum FSMState
{
	Idle =0,
	Quiet = 1,
	Walk= 2
}

that code doesnt Work … Probably get Bug when using StartCoroutine in Quiet Enum . But im not sure where it is , I think i have proper codes above .
All Replies will be very appreciated :grin:

Random.value gives you a float within 0.0 to 1.0. Casting it to int will floor that number so you have to get exactly 1 to have “probability” == 1.
I don’t think you want that.
Also, WaitForSeconds with 0 isn’t good, so WaitForSeconds with Random.value is dangerous.

Other than that, you should specify your “problems” a little further. Error messages in the console? Unexpected behaviour? Or a total crash?

Thank you , i just Forget add cast to int in MY post above . Actually i dont get any error just get unexpected Behaviour of my enemy … Now I change RayCast direction to be “tempatRaycast.right” and it works …but still can not firing a multiple Bullet in a second, i dont get any clue about that.
Thank you for reply anyway