Search Unity

Define intersection of two trigger colliders to render an effect

Discussion in 'VR' started by masta-yoda, May 20, 2020.

  1. masta-yoda

    masta-yoda

    Joined:
    Apr 19, 2020
    Posts:
    91
    I have two sword-like colliders in a game and want to render an effect when a user crossing them in the touch zone. My swords has trigger colliders (trigger = true) and are interacting with other world objects which has trigger colliders (trigger = true), in collision with other objects they are destroyed, or effects are rendered. The issue is when I try to render an effect of swords crossing when you touch each blades together. I'm getting the OnTriggerEnter/Stay/Exit events but unable to get touch points, these are for some reason available only in the OnCollisionEnter/Stay/Exit events which are not applicable for me as long as I'm using trigger colliders (this swords has lots of interactions with the world and I'm unable to make them trigger = false).

    The question is: having two given colliders (and associated game objects, meshes and rigid bodies), and knowing that the colliders has touched each other (OnTriggerEvent is called) how can I get the intersection of these two colliders. I want to render an effect at that point, or rather center point of the intersection (like sparks) but let them go through each other.

    4gTH3qj.png

    I've found lots of suggested solutions but none worked for me so far:

    1. Make them trigger = false, add non-kinematic rigit body and handle OnCollisionEnter - didn't work because it breaks the game mechanics with the rest of the world, swords now can be pushed by other objects and can push them instead of interacting by rules defined in the trigger event.
    2. Raycast - doesn't work, swords are not points, they are planes, so I need to get the whole collider intersection with the other
    3. BoxCast - the closest I was able to get, I'm sending BoxCasts to 4 sides of the sword (up, -up, right, -right) and it gives me approximate points but they are no precise and it doesn't work when swords are actually inside each other (you can swing a swords and pass through another blade)
    4. ClosestPoint from first collider to every point of the mesh vertices, and then get another collider and do the same and then find mid between two points. Didn't work either - the mesh is a cylinder and it doesn't have a lot of vertices in the middle of the sword mesh, the result is unaccepted.
    5. Vector3.Cross for both vectors of the swords - theoretically I can draw an vector from handle to the tip and cross it with another sword, but it gives me really weird result vector and I'm not sure how to turn it into the cross point between two colliders (or at least two vectors).

    It sounds like an standard 3d math problem but I'm having really hard time solving it :)
    Please advise.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Attempt 5 is closest. If you know the axis of each sword, then I think you just want to find the closest point between two lines (where each line is the axis of one of the swords).

    That is explained here (under "The shortest line between two lines in 3D"), and a C# implementation is here. This actually gives you a line segment which is the shortest one between the swords; you will probably want to generate your sparks at the midpoint of that.
     
    masta-yoda likes this.
  3. masta-yoda

    masta-yoda

    Joined:
    Apr 19, 2020
    Posts:
    91
    Thank you for the C# implementation of the algorithm. I've tried and it works perfectly in my test scene. I appreciate help and pointing me to the right direction. I still need to apply it to the main scene but I'm confident now that it have to work =)

    record_200520_211532.gif
     
    JoeStrout likes this.
  4. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,933
    If you have time: also file a bug with Unity for this. We've been asking for this feature for years, and the more people that log bugs the more chance someone at Unity will fix it :) - with VR, it might get more attention?. Someone eventually added the equivalent for Physics2D, if I remember correctly, but no-one's done it for 3D yet.