1
\$\begingroup\$

I have a component called "ReflectOffCollision". This component should obviously not need to be aware of specific details about the game such as tags. The problem is, I want to apply this script to an object that reflects off of only some collisions.

I could check the tag of the collision, but this will destroy the generality of the component. It will only work on collisions with this tag. I could have the script take a white list or black list of tags, but this is awkward to do.

My other option is to inherit from the component and make a more specific version. This seems like overkill, though.

How should I deal with tags in components? I see this problem anywhere that there are collisions. Am I using tags incorrectly, or misunderstanding them?

\$\endgroup\$
2
  • \$\begingroup\$ Does your object (which has this script) collide "normally" with Objects which would not trigger this Script? \$\endgroup\$ Oct 7 '14 at 14:22
  • \$\begingroup\$ Yeah. In this particular case, I want this object to reflect off of the level mesh, but behave differently when colliding with a player object. \$\endgroup\$
    – Ben
    Oct 7 '14 at 14:23
1
\$\begingroup\$

There are a few ways to achieve this. First, you can use layers and modify the collision matrix so that it won't interact with non-collidable objects. Second, objects you want to collide can contain a common component to mark them as collidable.

Let's say it's Reflectable. At run-time, you can determine whether the object is collidable or not with this:

if(collision.gameObject.GetComponent<Reflectable>()!=null)
    collide();
\$\endgroup\$
2
  • \$\begingroup\$ Wouldn't that just be an empty component that now has to be added to everything that's reflectable just for the sake of doing this? \$\endgroup\$
    – Ben
    Oct 7 '14 at 14:35
  • \$\begingroup\$ Yes, it would be an empty component. However, you can define a Movable base class and implement isReflectable() method and derive your controller classes from it also. \$\endgroup\$
    – Alican
    Oct 7 '14 at 14:44

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.