Hello people, I’ve been working over a roach and I recently started coding it’s AI…
I’m in progress and i’ve got a few problems with some of the code…
First of all, thats the code (C#):
using UnityEngine; using System.Collections; public class AI_roach : MonoBehaviour { public float distenemy; public bool combat=false; public float movespeed =5; public float rotationSpeed= 5; public float cooldown=0; public float hittype=0; public bool spotted = false; public float health = 100; void Start() { GameObject bloodfab = GameObject.Find("effect_blood"); Destroy(bloodfab,0); } void Awake() { animation["walk"].speed = 1; animation["attack2"].speed = 0.7f; animation["attack1"].speed = 0.7f; } void Update() { GameObject enemy = GameObject.Find("character_system"); distenemy = Vector3.Distance(enemy.transform.position,transform.position); if(combat && character_stats.health>0) { animation.Stop("walk"); Combat(); } if(distenemy>3 && spotted) { combat=false; WalkToEnemy(); } else if(distenemy<=3 && spotted) { animation.Stop("walk"); combat=true; } else Rest(); if(distenemy<10) { transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(enemy.transform.position - transform.position), rotationSpeed *Time.deltaTime); GameObject ply = GameObject.Find("character_system"); if (!Physics.Linecast (transform.position, ply.transform.position)&& !spotted) { spotted=true; } } } void WalkToEnemy() { transform.position+=transform.forward * movespeed * Time.deltaTime; animation.Play("walk"); } void Rest() { animation.Play("idle"); } // _____________COMBAT_________________ //_____________________________________ //_____________________________________ void Combat() { if(cooldown>0) cooldown-=1*Time.deltaTime; if(cooldown<=0) { hittype=Random.Range(0,4); if(hittype==3) ThrowHit(); if(hittype<3) RegularHit(); } if (!animation.isPlaying) animation.Play("idle"); if(character_stats.health<=0) combat=false; } void RegularHit() { animation.Play("attack2"); if(distenemy<3) { GameObject ply = GameObject.Find("character_system"); GameObject bloodfab = GameObject.Find("effect_blood"); bloodfab = Instantiate(bloodfab, ply.transform.position , ply.transform.rotation) as GameObject; character_stats.health-=Random.Range(3,15); } cooldown=1.5f; } void ThrowHit() { animation.Play("attack1"); if(distenemy<3) { GameObject ply = GameObject.Find("character_system"); GameObject bloodfab = GameObject.Find("effect_blood"); bloodfab = Instantiate(bloodfab, ply.transform.position , ply.transform.rotation) as GameObject; character_stats.health-=Random.Range(12,31); char_movesys.moveDir.y=15; } cooldown=3; } }
It’s in progress… Now ive got two problems. The first is the blood instantiating… It types NullReferenceException every time the roach is about to instantiate the blood (The hittin’).
Ive been trying to call the “gameobject” at the beggining of the code but when the prefabs code destroys itself once the gameObject var “looses” the prefab…
The sec prob is that the roach cannot detect the char when it is sppoused to be but only in some different types of rotations and positions… Like something blocks the raycast…
Many thanks for those who can help me out (=