- Home /
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;
}
}
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;
}
}
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.
Your answer
Follow this Question
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