Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kasigawa · Oct 19, 2013 at 10:43 PM · javascripttriggerbugenemysafe

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");


     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges