Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Making a Collider only collide with objects that don't share the same material color.

Discussion in 'Scripting' started by ProjectCryken, Dec 12, 2013.

  1. ProjectCryken

    ProjectCryken

    Joined:
    Jul 25, 2012
    Posts:
    41
    Without messing with physics layers(as I'd need different ones for every possible color) what is the best way to do this? I don't want to enable/disable the collider based on what is touching it as if two objects hit it at the same it would cause problems.

    Any suggestions appreciated, preferably in JS.
     
  2. SirWeeble

    SirWeeble

    Joined:
    Jul 20, 2013
    Posts:
    29
    Instead of using basic collision, use OnCollisionEnter() and give tags to your different objects based on material. Then you can create exceptions by checking for the tag in OnCollisionEnter()
     
  3. ProjectCryken

    ProjectCryken

    Joined:
    Jul 25, 2012
    Posts:
    41
    But then what do i do to make it not collide with that object?
     
  4. cjddmut

    cjddmut

    Joined:
    Nov 19, 2012
    Posts:
    178
  5. ProjectCryken

    ProjectCryken

    Joined:
    Jul 25, 2012
    Posts:
    41
    I have the following:

    function OnCollisionExit (thingHit : Collision)
    {
    if(thingHit.gameObject.renderer.material.color == renderer.material.color)
    Physics.IgnoreCollision(collider, thingHit.collider, false);

    if(thingHit.gameObject == player)
    manager.GetComponent(ColourChangeScript).switcherEnabled = true;
    }

    function OnCollisionEnter (thingHit : Collision)
    {

    if(thingHit.gameObject.renderer.material.color == renderer.material.color)
    Physics.IgnoreCollision(gameObject.collider, thingHit.collider, true);

    if(thingHit.gameObject == player)
    manager.GetComponent(ColourChangeScript).switcherEnabled = false;
    }


    It isn't ignoring the collisions at all. Any ideas?
     
  6. cjddmut

    cjddmut

    Joined:
    Nov 19, 2012
    Posts:
    178
    It appears to work for me when I test it out, I'd confirm that your conditional is triggering like you think it is with either a log statement or a breakpoint. Also, it might be worth considering to always turn back on collisions when you exit instead of doing a check otherwise you might get in a state where OnCollisionEnter won't be invoked when you expect it to?

    Though, while testing I noticed that even though I disable the collision between the two objects during OnCollisionEnter, there is still one frame where they collide and it can cause unwanted effects. You might be better off (if it's a relatively small number of possible colors) to place the object in a specific layer once its color changes and just set up to have layers ignore themselves with physics at the beginning of the game.

    If this sounds desirable, take a look at: https://docs.unity3d.com/Documentation/ScriptReference/Physics.IgnoreLayerCollision.html