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
    Commented Sep 10, 2020 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\$
    – user35344
    Commented Sep 10, 2020 at 15:21

1 Answer 1

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\$ Commented Aug 20, 2016 at 11:31
  • 4
    \$\begingroup\$ bounds.Contains accepts Vector3 as argument, not another bounds \$\endgroup\$
    – Daahrien
    Commented Jun 4, 2017 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
    Commented Sep 10, 2020 at 15:23
  • \$\begingroup\$ That answer is a bit more complicated than you need, this is simpler answers.unity.com/questions/292467/… \$\endgroup\$
    – jhocking
    Commented Sep 12, 2020 at 3:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .