Okay, I’ve been trying to code a turret all day, I have this theory that, for making an AI Turret, become aware Im near it, I’ll need a grid. I dunno how to code that. In my script there are two vars, that have no use yet, and I was going to use for the awareness grid. Like, the turret wont become aware once Im in the grid,only if I shoot my gun inside the grid/room, but when Im in the sight. Which brings me to the other var, I think Ill use a triangle, or something so, if I walk in front of it, then it will become aware.
Turret Script:
var Activated : boolean = true;
var ActivationSound : AudioClip;
var ActivationanimationString : String;
var awareGrid : GameObject;
var AwareSight : GameObject;
var Disabled : boolean = false;
var DisabledSound : AudioClip;
var DisabledanimationString : String;
var DisableTime : float = 5;
var DisableTimeRandomization: float = 0;
@HideInInspector
var countToTime : float;
var LookAtTarget : Transform;
var damp : float = 6.0;
var bullet : Transform;
var nextShotTime : float = 0.0;
var timeBetweenShots : float = 2.0;
var bulletSpawn : GameObject;
var bulletSound : GameObject;
var muzzelFlash : GameObject;
@HideInInspector
var playerTransform : Transform;
@HideInInspector
var cameraTransform : Transform;
var aware : boolean = true;
function Awake ()
{
//if (playerTransform)
//DisableTime += Random.value * DisableTimeRandomization;
if (aware)
playerTransform = GameObject.FindWithTag("Player").transform;
cameraTransform = GameObject.FindWithTag("MainCamera").transform;
AwareSight.GetComponent(Sight_Awareness).collisionDetected = true;
Activated = true;
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
}
if(Activated)
{
playerTransform = GameObject.FindWithTag("Player").transform;
cameraTransform = GameObject.FindWithTag("MainCamera").transform;
AwareSight.GetComponent(Sight_Awareness).collisionDetected = true;
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
animation.Play(ActivationanimationString);
if (ActivationSound)
audio.PlayOneShot(ActivationSound);
Aware = true;
}
if (Disabled)
{
AwareSight.GetComponent(Sight_Awareness).collisionDetected = false;
animation.Play(DisabledanimationString);
aware = false;
Activated = false;
if (DisabledSound)
audio.PlayOneShot(DisabledSound);
}
function Shoot()
{
if (bullet)
Instantiate(bullet,bulletSpawn.transform.position, bulletSpawn.transform.rotation);
if (bulletSound)
holdSound = Instantiate(bulletSound, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
if (muzzelFlash)
holdMuzzelFlash = Instantiate(muzzelFlash, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
if (nextShotTime <= Time.time)
{
Shoot();
nextShotTime = Time.time + timeBetweenShots;
var hits : RaycastHit[];
var anyHit : boolean = false;
if (Vector3.Angle(transform.forward, playerTransform.position - transform.position) !aware)
{
hits = Physics.SphereCastAll(transform.position, transform.localScale.x / 3, playerTransform.position - transform.position, Vector3.Distance(transform.position, playerTransform.position));
for (var hit : RaycastHit in hits)
{
if (hit.transform.tag == "Level Parts")
anyHit = true;
}
if (!anyHit)
aware = true;
}
}
}
The script is called “Turret_Control_Script” Please let me know if this theory will work, I am currently un-aware how to exactly code it, but I have an idea.
I also have another script for sight, called “Sight_Awareness”
The script:
var playerTransform : Transform;
@HideInInspector
var collisionDetected : boolean = true;
var Turret : GameObject;
function Awake ()
{
Turret.GetComponent(Turret_Control_Script).Aware = true;
collisionDetected = true;
}
function OnCollisionStay (collision : Collision)
{
if (collision.transform.tag == "Player")
collisionDetected = true;
Turret.GetComponent(Turret_Control_Script).Aware = true;
Turret.GetComponent(Turret_Control_Script).Activated = true;
Turret.GetComponent(Turret_Control_Script).Disabled = false;
}
function OnCollisionExit ()
{
collisionDetected = false;
Turret.GetComponent(Turret_Control_Script).Aware = false;
Turret.GetComponent(Turret_Control_Script).Disabled = true;
Turret.GetComponent(Turret_Control_Script).Activated = false;
}
They sorta work, but aren’t doing what I exactly hoped for. Please help