Hi have just do codes for several Hours …then get a problems , here it is
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