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 steelesound · Oct 30, 2014 at 10:39 PM · c#triggercoroutine

How to run a coroutine if in a trigger but run a different one if outside it?

It should be pretty obvious from the code below what I'm trying to do. I had accomplished something similar in JS but haven't got it quite figured out in C#

Essentially I am trying to start one coroutine if the object is in the trigger, but if it is not in the trigger it should start the other one.

Any help on this would be appreciated as I've been stuck on this one.

using UnityEngine; using System; using System.Collections;

 public class TriggerWwiseEvent_WepHit : MonoBehaviour {
     private float coolDown = 2.4f;
     private bool isPlaying = false;
     private bool isInTarget = false;
 
     
     //This coroutine should run if NOT in the trigger//
     void Update() {
         if (!isPlaying && Input.GetButtonUp("BasicAttack")){
             StartCoroutine("PlayPSNO");
         }
     }
 
 
 
     //This coroutine should run it IS in the trigger//
     void OnTriggerStay (Collider in_other) {
         if (in_other.tag == "Morde_Waist" && !isPlaying && Input.GetButtonUp("BasicAttack")){
             StartCoroutine("PlayPS");
         }
     }
 
 
 
 
 
     IEnumerator PlayPS() {
         isPlaying = true;
         yield return new WaitForSeconds(0.6f);
         GetComponent<AkTriggerEnterWeapon>().Keypress2();
         yield return new WaitForSeconds(coolDown);
         isPlaying = false;
     }
 
 
 
 
 
     IEnumerator PlayPSNO() {
         isPlaying = true;
         yield return new WaitForSeconds(0.6f);
         yield return new WaitForSeconds(coolDown);
         isPlaying = false;
     }
 
 
 
     
 }
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 steelesound · Oct 31, 2014 at 03:37 AM 0
Share

I have solved it, kind of! but now a new question.. first here is my working script now:

Now the issue I'm having is that sometimes this :

 void Update() {
     if (!isPlaying && Input.GetButtonUp("BasicAttack")){
         StartCoroutine("PlayPST");
     }
 }

gets carried out when really the other one should be carried out. How can I give:

    void OnTriggerStay (Collider in_other) {
     if (in_other.tag == "$$anonymous$$orde_Attack1" && !isPlaying && Input.GetButtonUp("BasicAttack")){
         StartCoroutine("PlayPS");
     }
 }

The priority when it's conditions are met?

using UnityEngine; using System; using System.Collections;

 public class TriggerWwiseEvent_WepHit : $$anonymous$$onoBehaviour {
     private float coolDown = 2.4f;
     private bool isPlaying = false;
     private bool isInTarget = false;
 
 
 
 
 
 
 
     void Update() {
         if (!isPlaying && Input.GetButtonUp("BasicAttack")){
             StartCoroutine("PlayPST");
         }
     }
 
 
     //This coroutine should run it IS in the trigger//
     void OnTriggerStay (Collider in_other) {
         if (in_other.tag == "$$anonymous$$orde_Attack1" && !isPlaying && Input.GetButtonUp("BasicAttack")){
             StartCoroutine("PlayPS");
         }
     }
     
 
     
 
 
 
     IEnumerator PlayPS() {
         isPlaying = true;
         yield return new WaitForSeconds(0.6f);
         GetComponent<AkTriggerEnterWeapon>().$$anonymous$$eypress2();
         yield return new WaitForSeconds(coolDown);
         isPlaying = false;
     }
 
 
     IEnumerator PlayPST() {
         isPlaying = true;
         yield return new WaitForSeconds(0.6f);
         yield return new WaitForSeconds(coolDown);
         isPlaying = false;
     }
 
 
 
     
 }





1 Reply

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

Answer by devluz · Oct 31, 2014 at 03:44 AM

I would recommend something like that (untested example):

 private bool itIsInTrigger = false;


 void Update()
 {

     if (!isPlaying && Input.GetButtonUp("BasicAttack"))
     {
         if (itIsInTrigger)
         {
             //call routine 1
             
         }
         else
         {
             //call routine 2
         }
     }
 }
 void OnTriggerEnter(Collider in_other)
 {
     if (in_other.tag == "Morde_Attack1")
     {
         itIsInTrigger = true;
     }
 }

 void OnTriggerLeave(Collider in_other)
 {
     if (in_other.tag == "Morde_Attack1")
     {
         itIsInTrigger = false;
     }
 }

Not sure if you should call coroutines in such a high frequency. There is maybe room for optimization.

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 steelesound · Oct 31, 2014 at 10:10 PM 0
Share

Worked like a charm, thank you!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Want health to decrement longer stays on trigger 2 Answers

Activate/Deactivate UI Image using C# 0 Answers

Trigger not getting GameObject 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