Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Conditional color change on collision

Discussion in 'Physics' started by jasonrward, Aug 18, 2022.

  1. jasonrward

    jasonrward

    Joined:
    Nov 22, 2021
    Posts:
    7
    I know that I've seen similar topics in here, but none of them seem to be quite what I need.

    I'm trying to create a setup where different color spheres are moving inside a box. Let's say blue, red, green, yellow.

    If blue and red collide, they change colors to green and yellow.
    If green and yellow collide, they change color to blue and red.
    Any other combination, and they just bounce off of each other.
    The total number of spheres needs to remain constant.

    If I've read the other threads correctly, the issue is that one object will has to react first. So if blue and red collide, blue might turn green, but then the trigger on red no longer happens because it registers green? Is there a way around this?
     
  2. pixaware_pwedrowski

    pixaware_pwedrowski

    Joined:
    Oct 25, 2018
    Posts:
    116
    You can try to delay color change for say 0.1s. Not sure if it is the best solution, but will probably work
     
  3. alex101010000

    alex101010000

    Joined:
    Apr 12, 2021
    Posts:
    4
    Store the last position of every sphere around a ball (use a trigger around it or something) and the last color. If a sphere
    of different color is located where on the previous frame there was a sphere, you can be sure that it is the sphere from the previous frame. I suggest u write this code inside FixedUpdate
     
  4. jasonrward

    jasonrward

    Joined:
    Nov 22, 2021
    Posts:
    7
    Thanks for the suggestions. I ended up destroying both objects and spawning two new objects with the appropriate colors. Seems to work well enough.