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 newcomix · Jun 16, 2013 at 09:59 PM · c#gameobjectenemyswitchcase

Why does only one object respond, while more objects have exact same script?

I have some enemies who are suppose to attack when a certain amount of triggerpoints have been reached. The triggerpoints are added by collision of player and item which is held in a different script.

They start Unaware, always facing the player with their backs. If the player colliders with an item 2x they should become alerted (this is kept track of with the triggerpoints). If they continue to collide with items they will attack.

I have the same script on my enemy, and i have several of them spread throught the rooms. The problem is only one of them looks at the player when they switch to alerted.

[code] using UnityEngine; using System.Collections;

public class EnemyAI : MonoBehaviour { public enum MonsterState // de states { unaware, alerted, attack

 } ;
 
 //______variabelen________________________
 
 public Transform target;                 // vergeet niet de player een tag te geven
 public int moveSpeed;

 public int maxDistance;
 public int medDistance;
 public int minDistance;
 public MonsterState gameState;            // dit is een variable om de state aan te roepen als parameter ofzo
  
 private Transform myTransform;
 private CharacterController controller; // anders beweegt ie niet, is als component aan de enemy toegevoegd


 private int triggerPoint;                
 private int unawareTrigger;
 private int alertedTrigger;
 private int attackTrigger;
 public     int deKamer;
 
 // audio
 
 public AudioClip au_suspense;
 private bool switchState;
 private    bool tooclose;
 private float monstCooldown;
 private float monstTimer; 
 public int kamerNummer;

 public AudioClip au_monsterClose;
 


 //_____awake______________________________
 
 void Awake ()
 {
     myTransform = transform;            // wordt gecached zodat hij altijd de transform info heeft
     controller = GetComponent();


 }
 // _ OnGUID __________________________
 

// void OnGUI () // { // // GUI.Label (new Rect(10,110,300,30),"Triggers: " + triggerPoint + "/4"); //dit laat de score zien in tekst // //
// }

 //_____start______________________________
 
 void Start ()
 {
     //wanneer hetspel begint vindt de enemy de speler eerst
     GameObject go = GameObject.FindGameObjectWithTag ("Player"); // tag de enemy en de player in inspector
     target = go.transform;            // dit is weer een variable die later wordt gebruikt en hier wordt de target opgeslagen van de findtag                            //go is de naam van de gameobject
     minDistance = 2;
     medDistance = 3;                // dit is een andere distance omdat ie anders vanaf alerted meteen switched naar attack, nu geven we de speler een kans terug te gaan
     maxDistance = 4;                //zo blijven ze op afstand
     unawareTrigger = 1;
     
     attackTrigger = 4;
     alertedTrigger = attackTrigger-1;
     switchState = true;
     moveSpeed = 1;
     
     monstTimer = 0;            
     monstCooldown = 5.0f;
     tooclose = true;
     
     controller = GetComponent ();// dit is nodig omdat deze variabele straks wordt gebruikt, en dit roept het aan
     
 }
 

 
 //_____update_____________________________
 
 void Update ()
 {
     
     
     
     if (monstTimer > 0)
         monstTimer -= Time.deltaTime; // every time  attacktimer is greater then 0 it will cooldown in that time
             
         if (monstTimer < 0)
             monstTimer = 0;
         
         if (monstTimer == 0)
         tooclose = true;
         
     deKamer = GameObject.Find("De_checker").GetComponent().deKamer();
     
     
     
     //if toggle is on
     //de states worden continue gechecked of ze van toepassing zijn
     switch (gameState)
      {
     
     case MonsterState.unaware:            // let op dubbele punt niet punt komma
         Unaware ();                        // hier wordt de functie ingevoerd die actief is in deze state, voer hier alle andere functies en if statements in
         break;
         
     case MonsterState.alerted:            
         Alerted ();                        
         break;
         
     case MonsterState.attack:            
         Attack ();                        
         break;
     }    
         
     


 }
 // de triggerpoint unawareTrigger =1;        alertedTrigger = attacktrigger -1;         attackTrigger = 4;         totalTrigger = 0;
 
 public void Reset()
 {
     triggerPoint = 0;
     Debug.Log ( "Doet dit het");
     
 }
 
 public void TriggerCount ()                    
 { 
     
     triggerPoint++;
     
     // sound feedback 1x played
     
     if (triggerPoint ==    unawareTrigger+1 && switchState == true)
     {    
         audio.PlayOneShot(au_suspense);
         audio.PlayOneShot(au_monsterClose);
         switchState = false;
     }
     
     if ( triggerPoint == attackTrigger && switchState == true)
     {
         audio.PlayOneShot(au_suspense);
         audio.PlayOneShot(au_monsterClose);
         switchState = false;
     }
     
     // trigger points which switch to states
     if(triggerPoint > unawareTrigger && triggerPoint < attackTrigger)    // 2 3 
     {
         
         gameState = MonsterState.alerted;
         Debug.Log ("switch to alerted");
         switchState = true;
     }
     
     else if(triggerPoint > alertedTrigger )                                // 4 +
     {
         
         gameState = MonsterState.attack;
         Debug.Log ("switch to attack");
     }
     
     else                                                                // 0 1 2
     {
         gameState = MonsterState.unaware;
         Debug.Log ("switch to unaware");
     }

 }
 
 private void Unaware()
     {
     //idle
     // de movement tijdens unaware - monster blijft vanaf maxdistance altijd met zijn rug naar de speler
     
     if (Vector3.Distance (target.position, myTransform.position) < medDistance)     
             {
                 if (tooclose == true)
             {
                 audio.PlayOneShot(au_monsterClose);
                 monstTimer = monstCooldown;
                 tooclose = false;
             }
         
             Vector3 moveDirection = target.transform.position - transform.position;
             moveDirection.Normalize ();
             moveDirection.y = 0;
             
                             
             transform.LookAt (transform.position + moveDirection); 
             gameState = MonsterState.alerted;
             }
             
     // if the distance is greater the player wont be noticed
         if (Vector3.Distance (target.position, myTransform.position) > maxDistance) 
             {
             Vector3 moveDirection = target.transform.position - transform.position;
             moveDirection.Normalize ();
             moveDirection.y = 0;                                            // zo gaat ie niet draaien
                             
             transform.LookAt (transform.position - moveDirection); 
             }
             
     
     }
 
 private void Alerted()
     {
     // kijkt naar de speler - maakt geluid - muziek wordt spannender - licht en donker?    
             Vector3 moveDirection = target.transform.position - transform.position;
             moveDirection.Normalize ();
             moveDirection.y = 0;                                            // zo gaat ie niet draaien
                             
             transform.LookAt (transform.position + moveDirection);
     
     // het monster kijkt al naar de speler: als de speler nu nog dichterbij komt dan gaat ie aanvallen
             if (Vector3.Distance (target.position, myTransform.position) < minDistance || Vector3.Distance (target.position, myTransform.position) < medDistance && GameObject.Find("PlayerSpotLight").GetComponent().lampcheck() )     
             {
                 gameState = MonsterState.attack;
             }

     }
 
 private void Attack ()
     {
             // dit berekent de move tussen enemy en player  - bepalend op de maxdistance
             // de movement tijdens alerted - het monster gaat naar de speler toe (helaas tot op zijn hoofd) 
         if (deKamer == 2 || deKamer == 5)     
             {
                 Vector3 moveDirection = target.transform.position - transform.position;

                 moveDirection.Normalize ();
                 moveDirection.y = 0;                                            // zo gaat ie niet draaien

     
                 controller.Move (moveDirection * moveSpeed * Time.deltaTime);    // hier vlgt/beweegt ie de controller functie boven aangeven met getcomponent
                 transform.LookAt (transform.position + moveDirection);             //hierdoor blijft ie gericht op de Lookat target en draaitt ie langzaam die richting op
             }
         
     }
 
 
 
 

}

[/code]

Theres comments in dutch.. sorry...

Comment
Add comment · Show 1
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
avatar image newcomix · Jun 16, 2013 at 09:51 PM 0
Share

im not actually using the last section where it says (de$$anonymous$$amer == 2 || de$$anonymous$$amer == 5 ) I just put that there to test so they might activate in the rooms (dekamer = theroom). The test failed btw.

So to recap the question:

Why does only one gameobject reacts on the script while they all have the exact same script ( theyre even prefabs but i now broke the prefab conncetion to see if that works. )

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

Switch() problem [C#] 1 Answer

Space Shooter Hazards Not Spawning 0 Answers

Distribute terrain in zones 3 Answers

How to check if an enum’s case has changed 3 Answers

Updated Question if statement not executing case 1 Answer


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