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
1
Question by XenoAisam · Sep 11, 2012 at 09:43 AM · ainpcgrenadeavoidance

Issues NPC Grenade avoidance

Hello this is my first time here, I have an issues about NPC especially for Grenade Avoidance. Let take example: NPC see grenade near them --> They avoid it by going out of range--> The Script work! but when about to shout because awareness of "Grenade!!". It's not working as expected. Here example:

 Function GrenadeRun(){        
      while (true) {
             
          if ( GrenadeDistance < AwareGrenadeRange ) {                            
        //Shout grenade "GRENADE!" script
         }    
 
          if ( GrenadeDistance < RunGrenadeRange ) {                            
         //Run from grenade
         }
     yield;    
     }
 }

The script result: NPC run away from grenade but shout the "grenade!" frame-by-frame. Undesired result. So i try this way:

 Function GrenadeRun(){        
      while (true) {
             
          if ( GrenadeDistance < AwareGrenadeRange ) {                            
        //Shout grenade "GRENADE!" script
             yield WaitForSeconds(1);
         }    
 
          if ( GrenadeDistance < RunGrenadeRange ) {                            
         //Run from grenade
         }
     yield;    
     }
 }

The script result: The NPC shout "grenade!" is ok now but need to wait 1 second before start run away from grenade. Undesired result. Another attempt:

  Function GrenadeRun(){        
            while (true) {
                 
              if ( GrenadeDistance < AwareGrenadeRange ) {                            
            //Shout grenade "GRENADE!" script
                 yield WaitForSeconds(1);
             }
         }    
          while (true) {
              if ( GrenadeDistance < RunGrenadeRange ) {                            
             //Run from grenade
             }
         yield;    
         }
     }

The script result: The NPC only shout "GRENADE!" only. Not running from grenade What i want is the NPC shout "Grenade!" per second and start to run simultaneously. but i still cant figure it out. Thank you

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 Fattie · Sep 11, 2012 at 12:37 PM 0
Share

+1 for impressive use of acronyms

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Azran · Sep 11, 2012 at 02:42 PM

The problem here is that you are calling shout inside the loop.

You can use a variable which hold the last time you shouted. Here is my pseudocode

 if (lastTimeShouted < Time.Now - 1sec)
 {
     shoutAgain();
     lastTimeShouted = Time.Now
 }
Comment
Add comment · Show 1 · Share
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 XenoAisam · Sep 11, 2012 at 03:15 PM 0
Share

Your solution is work

avatar image
0

Answer by XenoAisam · Sep 11, 2012 at 01:16 PM

i have tried it.. its still not working. It's still produce "Grenade!" frame-by-frame. To be honest, i still want to retain the use of "while(true}" in the code: here my code:

 function GrenadeRun () {
 var curWayPoint = AutoWayPoint.FindClosest(transform.position);
 
     var lastVisibleGrenadePosition = GrenadeTar.transform.position;
 if (CanSeeGrenade ()){
     while (true) {
 
 
             var waypointPosition = curWayPoint.transform.position;
     
             if (Vector3.Distance(waypointPosition, transform.position) < pickNextWaypointDistance)
                 curWayPoint = PickNextWaypoint (curWayPoint);
                
             var distance = Vector3.Distance(transform.position, GrenadeTar.transform.position);
             
             
             if ( distance < AwareGrenadeRange ) {                        
                 GrenadaWarning();
                 }
             
             if ( distance < AwareGrenadeRange) {
                 MoveTowardsRun(waypointPosition);
                 } else {
                 BackToPatrol ();
                 return;
                 }    
                 
             if (CanSeeTarget () && !CanSeeGrenade()) {
                 yield StartCoroutine("AttackPlayer");
                 return; 
                 }
                                 
                                                                                                                                                                                         
         yield;            
         }
     }    
 }

And the targeted function of GrenadaWarning();

 function GrenadaWarning () {
 
 var distance = Vector3.Distance(transform.position, GrenadeTar.transform.position);
 
 while (distance < AwareGrenadeRange) {
 BroadcastMessage("GrenWarnStarter");
     yield WaitForSeconds(1.5);
     }
 }

Still make the NPC shout continuously frame-by-frame

Comment
Add comment · Share
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
0

Answer by XenoAisam · Sep 11, 2012 at 03:19 PM

Azran answer solved my issues :D Thank you Azran. Next time i will try to learn more about unity script especially Invoke timing too.

here the working script:

declare at header (JavaScript):

 private var lastTimeShouted = 0.0;

The Shout one:

 function GrenadaWarning () {
 
 if (lastTimeShouted < Time.time - 1)    
 BroadcastMessage("GrenWarnStarter");
     lastTimeShouted = Time.time;
 
 }

i feel very shame.. its just a few line too but work as my expecting

Comment
Add comment · Show 1 · Share
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 Fattie · Sep 11, 2012 at 04:37 PM 0
Share

You really need to look at the incredibly simple "Invoke" command. there is no reason to use Yield, etc for such simple situations.

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

11 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

Related Questions

A* or AStar Pathfinding doesnt work for me? 0 Answers

without using NavMesh, How to avoid autonomous moving agents/obstacles 1 Answer

NPC has shootable crossbow. How to get his arms and weapon to point at player when shooting? 0 Answers

Make NPC follow player when seen? 2 Answers

help setting a Npc to attack multitple enemies 0 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