0
\$\begingroup\$

I am trying to create a "Fruit Ninja" style game in Unity in VR. I have a BoxCollider on my sword set to "Trigger" and I have some "fruit" objects coming towards it. I then have this function:

void OnTriggerEnter(Collider other)
{
    Destroy(gameObject);
    Destroy(other.gameObject);
}

I've tried applying this script to the sword and to the objects, but nothing is destroyed.

In the documentation, there is also a note of "Trigger events are only sent if one of the Colliders also has a Rigidbody attached", but I've attached a Rigidbody to the objects.

\$\endgroup\$
  • \$\begingroup\$ Can you include a screengrab showing how you've set up your objects, and a freeze-frame in the scene where their colliders are intersecting? Also, remember to show us how you're moving your object and sword. \$\endgroup\$ – DMGregory Jun 27 '19 at 15:03
  • \$\begingroup\$ I figured out my issue, the Rigidbody can't be set to Kinematic. This doesn't seem to be in the documentation. \$\endgroup\$ – Aaron Franke Jun 27 '19 at 15:17
  • 1
    \$\begingroup\$ It is. Read the collision action matrix at the bottom of this page. If you've solved your problem, feel free to share your solution as an Answer. \$\endgroup\$ – DMGregory Jun 27 '19 at 15:23
1
\$\begingroup\$

Can't write a comment, so I will give you my pointers.

You should not apply that script to every object, you're basically destroying both the sword and the fruits. I think you should only place your script in the sword and only destroy the collided object. Thus, your script will change to the following :

void OnTriggerEnter(Collider other)
{
    Destroy(other.gameObject);
}

Basically, it will destroy every object entering in contact with your sword. Furthermore, if you want more liberty, you can change the tags of your objects and make sure to only destroy objects tagged as fruits or even have different effects. For example:

void OnTriggerEnter(Collider other)
{
    if (other.tag == "Fruit") 
    {
        playerPoints++;
    }
    else 
    {
        playerPoints--;            
    }
    Destroy(other.gameObject);
}

Best of luck in your development!

| improve this answer | |
\$\endgroup\$

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.