- Home /
What’s wrong with my Safe Volume (safe zone) code?
I’ve had this problem for weeks now. I've researched, redone my logic, tried different methods, tried putting the code in different places, and nothing seems to be working. The problem is with my safe volume. The enemy chases the player and stops when the player enters the safe volume. When the player gets out of the safe volume, the enemy remains idle even when the player is in the enemies view (the enemy's view is also a trigger volume; detect and detect2 are called variables from this trigger). The gameplay is supposed to be that when the player goes into the safe volume, he is safe; even if the enemy is chasing him. If the player gets out of the safe volume, and is also inside the enemy's view, the enemy is suppose to chase him. Furthermore, to me, the code makes logical sense, I don’t know why it doesn't work. How can I fix this or do something different? In addition, I have tried to use the OnTriggerStay, but that doesn't seem to work. Could there be something wrong with the OnTriggerExit in the safe volume code (or maybe a bug using this function)? Please help me figure this out, I’m at my last straw about continuing this game. Please look at the code below, I would greatly appreciate it if anyone could help me. Thank you.
//ENEMY CODE//
#pragma strict
var detectedCheck : EnemyVolumeOne;
var willAttackCheck : boolean;
var target : Transform;
var moveSpeed = 20;
var rotationSpeed = 5;
var myTransform : Transform;
var enemyLevelCollision : EnemyVolumeTwo;
var safeZoneCheck : SafeZone;
function Awake(){
myTransform = transform;
}
function Update () {
//calling resources from volumes(don't touch)
enemyLevelCollision = GetComponentInChildren(EnemyVolumeTwo);
detectedCheck = GetComponentInChildren(EnemyVolumeOne);
safeZoneCheck = GameObject.FindGameObjectWithTag("SafeZone").GetComponent(SafeZone);
//Idle loop animation(don't touch)
if (!animation.isPlaying){
animation.Play("EvilScientist_Idle");
}
//detect sequence START// (works)
//if condition(detected) is true, enemy walks toward player.(works)
if(detectedCheck.detected == true){
enemyMove();
}
// add code later: add else statement to make enemy idle; add loop; perhaps add a switch statement. Also add navmesh.
//calling from volumes (works)
if (detectedCheck.detected2 == true){
enemyMove();
}
if (safeZoneCheck.safe == true){
detectedCheck.detected = false;
detectedCheck.detected2 = false; //this does not work!
}
/*ignore code: if player gets out of volume and is not in detectvolume1
if(safeZoneCheck.safeCheck2 == true && detectedCheck.detected == true){
enemyMove();
detectedCheck.detected2 = true;//check if needed
}*/
//detect sequence END//
//enemy can attack.(works; don't touch)
if (enemyLevelCollision.enemyWillAttack == true){
animation.Play("EvilScientist_Attack01");
print("Attack!!!");
}
}
//enemy movment (works; don't touch)
function enemyMove(){
transform.LookAt(Vector3(target.position.x,transform.position.y,target.position.z));
myTransform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
animation.Play("EvilScientist_Walk");
}
//SAFE VOLUME//
#pragma strict
var detectionCheck : EnemyVolumeOne;
var safe : boolean;
var safeCheck2 : boolean; //r
function Update () {
}
//if player enters the trigger, he is undetected and hidden. (works)
function OnTriggerEnter(other : Collider){
if(other.tag == "Player"){
detectionCheck = GameObject.FindGameObjectWithTag("MadScientist").GetComponentInChildren(EnemyVolumeOne);
//detectVolume2.GetComponent(detected);
detectionCheck.detected = false; //ref enemyAIScript
safe = true;
print("You are safe");
}
}
//if player exits the trigger, he can be detected. //(doesn't work, not sure why)
function OnTriggerExit(other : Collider){
if(other.tag == "Player"){
safe = false;
//safeCheck2 = true; (keep just in case)
print("You are vulnerable");
}
}
Your answer
Follow this Question
Related Questions
On TriggerEnter issues 1 Answer
AI getting stuck on corners 1 Answer
why won't my code work? 1 Answer
2d Enemy JavaScript 0 Answers
Fade out effect isn't working if loaded at the second time. 2 Answers