• 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 /
  • Help Room /
avatar image
Question by MonsterGenome · Oct 17, 2016 at 05:56 PM · unity 5ienumeratorteleportenemiesslender

How do I get an entity's IEnumerator to pause when the entity is seen?

I've got a villain type who teleports and inches towards the player unless they are seen, but I can't seem to figure out how to make them stop teleporting and moving when they are being rendered. I tried to find a way to use an inverted version of Renderer.isVisible, but couldn't make it work.

Here's the teleporter code as-is: void Start () { ballotBits = 0; rb = GetComponent(); StartCoroutine(SlenderWalk()); }

     IEnumerator SlenderWalk()
     {
         yield return new WaitForSeconds(startWait);
         while (true)
         {
             if (ballotBits == 0)
             {
                 teleDist = Random.Range(startDist.x, startDist.y);
             }
             if (ballotBits == 1)
             {
                 teleDist = Random.Range(firstDist.x, firstDist.y);
             }
 //.....
                     if (ballotBits == 7)
             {
                 teleDist = Random.Range(seventhDist.x, seventhDist.y);
             }
             if (ballotBits == 8)
             {
                 teleDist = Random.Range(eighthDist.x, eighthDist.y);
             }
             teleAngle = Random.Range(player.transform.eulerAngles.y+90f,player.transform.eulerAngles.y+270f);
             PolarToCartesian(teleDist, teleAngle, out teleLoc);
             rb.transform.position = new Vector3 (
                 player.transform.position.x + teleLoc.x,
                 Terrain.activeTerrain.SampleHeight(new Vector3(player.transform.position.x + teleLoc.x, 0, player.transform.position.z + teleLoc.y)) + 1.9f,
                 player.transform.position.z + teleLoc.y
                 );
             yield return new WaitForSeconds(teleWait);
         }
 
     }
     public static void PolarToCartesian(float radius, float polar, out Vector2 outCart)
     {
         polar = polar * Mathf.PI / 180;
         outCart.x = radius * Mathf.Cos(polar);
         outCart.y = radius * Mathf.Sin(polar);
     }

I think that once I know how to pause this It'll be fairly simple to make it work in order to stop them from walking by just applying the same principles to the walking script.

Comment

People who like this

0 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 TBruce · Oct 17, 2016 at 08:16 PM 1
Share

Why are your Random.Range() calls using x and y vector values? The x and y vector values could be easily equal or the y value could be less

Why are you adding Vector3.z values to Vector2.y.

But to answer your question (and without anymore code to go by) you can do something like this (name variables and types to what you want)

 public enum  WalkingStatus {CancelWalking, Inactive, StartWalking, IsWalking};
 
 // default to inactive
 private WalkingStatus walkingStatus = WalkingStatus.Inactive;
 
 void Start()
 {
     // set status to StartWalking
     // this will start the coroutine in Update
     // and change the status to IsWalking
     SetWalkingStatus(WalkingStatus.StartWalking);
 }
 
 void SetWalkingStatus(WalkingStatus status)
 {
     walkingStatus = status;
 }
 
 void Update()
 {
     if (walkingStatus == WalkingStatus.CancelWalking)
     {
         StopCoroutine("SlenderWalk");
         SetWalkingStatus(WalkingStatus.Inactive);
     }
     else if (walkingStatus == WalkingStatus.StartWalking)
     {
         SetWalkingStatus(WalkingStatus.IsWalking);
         StartCoroutine(SlenderWalk);
     }
 }

To cancel/pause the coroutine just call

 SetWalkingStatus(WalkingStatus.CancelWalking);

where needed (if you are doing this in Update, do it above the code provided).

Inside SlenderWalk() change this

 while (true)

to this

 while (walkingStatus == WalkingStatus.IsWalking)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

115 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 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 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 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

Translating Ienumerator from seconds into minutes? 0 Answers

Can I use the IEnumerator in my example? (If yes, how?) 0 Answers

Struggling with creating a Slender-like AI (Creating this for educational purposes) 0 Answers

How can I make this character move smoothly? 1 Answer

The right way using Coroutine and Ienumerator 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