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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to make this in unity?

Discussion in '2D' started by cuentaalternativa000, Apr 23, 2023.

  1. cuentaalternativa000

    cuentaalternativa000

    Joined:
    Apr 4, 2020
    Posts:
    2
    Hello hello!
    I wanted to do the following effect but I couldn't, if anyone knows how to do it and can help me I would appreciate it.

    The idea is, depending on the collision of two objects (sprites), "object1 is the green rectangle" and "object 2 is the red", object2 would have to know the point at which (creating an imaginary line) object1 would come out on the other side of the object.

    I wanted to create this effect to be able to calculate where an object enters and exits and to be able to change the texture of the object2 itself at the entry and exit point

    upload_2023-4-23_12-55-46.png
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,457
    Using add force should just work if you use rigidbodies.
    Otherwise look into lerp/slerp/smoothdamp i think
     
  3. cuentaalternativa000

    cuentaalternativa000

    Joined:
    Apr 4, 2020
    Posts:
    2
    Yes, I thought about it but I would need to know the exact two points to change the texture at that point
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    You mention "sprites colliding" which, of course, they don't, colliders do so I'm going to assume you'll use a PolygonCollider2D only in which case, the attached project shows you how to perform an intersection clipping that retrieves all the intersections of a single ray which you can substitue for a ray at the hit point in the direction of the velocity you were moving before you came into contact.

    This gathers the clip-points then sorts them time-wise with the first being the first hit and the last being the last hit. In your case you only want these so you can simply use them.

    This gives you intersection points. In terms of textures, that's obviously nothing to do with physics so you'd have to correlate the clip-points with the sprite and texture yourself.

    Clearly though, this is for a ray enter/exit which isn't your other collider shape. You can do this another way which is, instead of a ray, you perform a physics query such as BoxCast to perform a check in in the opposite direction back along that ray to see where it hits the other side.

    Note: If you want this for any type of collider then things get much more complex.

    Hope this helps.

    PolygonClip.png
     

    Attached Files: