Home c# Working with ONTRIGGERSTAY

Working with ONTRIGGERSTAY

Author

Date

Category

In general, trigger does not correctly work (perhaps I don’t know something), when using such code:

void ontriggerstay (collider other)
{
    if (input.getKeydown (LootButton))
    {
      Debug.log ("Buttonwaspushed");
    }
}

When I am in the collider of the object and click on the button – the log is duplicated (the same message is displayed 2 times), plus everything, not all pressures are generally perceived, that is, I can press 5 times, and from five clicks May not go out at all. (With all ok button, checked on others)


Answer 1, Authority 100%

could immediately put the question in this way.

Public Class Loot: Monobehaviour {
  Public void Taking () {
    DESTROYIMMEDIATE (GameObject);
  }
}
Public Class Player: Monobehaviour {
  Private List & lt; Loot & GT; _Closeloot = New List & lt; Loot & gt; ();
  Private void Update () {
    if (input.GetKeyDown (keycode.space) & amp; & amp; _closeloot.count & gt; 0) {
      _Closeloot [0] .taking ();
      _Closeloot.Removeat (0);
    }
  }
  Private void onNigGeNTER (Collider Other) {
    If (Other.getcomponent & lt; Loot & gt; ()! = NULL)
      _closeloot.add (other.getcomponent & lt; loot & gt; ());
  }
  Private Void OnTriggereExit (Collider Other) {
    If (Other.getcomponent & lt; Loot & gt; ()! = NULL)
      _Closeloot.Remove (Other.getcomponent & lt; Loot & gt; ());
  }
}

Answer 2, Authority 100%

Perhaps something in the first location of the documentation:

Ontriggerstay IS Called Almost All The Frames for Every Collider Other That Is Touching The Trigger. The Function Is On The Physics Timer SO it Won’t Necessarily Run Every Frame.

And even though not in the first paragraph, but about how input is also written:

Note Also That The Input Flags Are Not Reset Update. IT IS SUGGESTED YOU MAKE ALL THE INPUT CALLS IN THE UPDATE LOOP.

They have different frequencies and ontriggerstay to put it mildly not a place for Input .


Answer 3

found an alternative to who is interested:

void update ()
  {
    if (input.getKeydown (LootButton) & amp; & amp; intrigger == true)
    {
      WaspUSHED = TRUE;
    }
  }
  Void Ontriggerstay (Collider Other)
  {
    Intrigger = True // You can also put on the ontriggerenter, I think the role is not playing strongly.
    If (WaspUSHED == TRUE)
    {
      Loot ();
      waspushed = false;
    }
  }
   Void OntriggereExit (Collider Other)
   {
    intrigger = false;
   }

It turns out that in the update when you press the button – the BOOL changes on true and in the trigger (when the character in it) is checked if the Bool True is if my loot () method is processed, and the BOOL variable becomes FALSE. Somehow, I think there is still a solution, but this is the first thing that occurred to.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions