2
\$\begingroup\$

How to make that when object is fully in collider, just that moment "something happens", now I only know triggering.Hmm I mean when that cube is fully in the collider. enter image description here

\$\endgroup\$
2
  • \$\begingroup\$ @Tyyppi_77 I tried if (other.bounds.Contains(this.bounds)) but it is showing error at (this.bounds) How to resolve that error? \$\endgroup\$
    – Naeem
    Sep 10 '20 at 12:48
  • \$\begingroup\$ @Naeem See my edit to the answer, apparently modern game engines do not bother to provide reasonable method overloads for collision checks. \$\endgroup\$
    – Tyyppi_77
    Sep 10 '20 at 15:21
2
\$\begingroup\$

You can use the OnTriggerStay callback, and make sure that you don't trigger the action before the collider is fully container by the container. So basically you just need an extra check inside OnTriggerStay.

void OnTriggerStay(Collider other)
{
    if (other.bounds.Contains(this.bounds.min) && other.bounds.Contains(this.bounds.max))
    {
        PerformAction();
    }
}

Unity doesn't provide a standard functionality for this, but this very basic approach should do the job.

\$\endgroup\$
4
  • \$\begingroup\$ This one will work great. Another way is with raycast distance without getting into collision stuff. \$\endgroup\$ Aug 20 '16 at 11:31
  • 4
    \$\begingroup\$ bounds.Contains accepts Vector3 as argument, not another bounds \$\endgroup\$
    – Lestat
    Jun 4 '17 at 1:40
  • 2
    \$\begingroup\$ You can write an extension method for this if it's something you'd like as a single method call. \$\endgroup\$
    – DMGregory
    Sep 10 '20 at 15:23
  • \$\begingroup\$ That answer is a bit more complicated than you need, this is simpler answers.unity.com/questions/292467/… \$\endgroup\$
    – jhocking
    Sep 12 '20 at 3:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.