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

Question 2D colliders in a 3D environment

Discussion in '2D' started by DrVanillaCookie, Aug 25, 2023.

  1. DrVanillaCookie

    DrVanillaCookie

    Joined:
    Aug 23, 2021
    Posts:
    39
    This probably have been asked before, but here's the thing: I'm basically trying to imitate Don't Starve camera style; so there's a 3D worldspace and every object is a 2D sprite. The camera is in Perspective and tilted by 45 degrees and every object has to be at 45 as well. It all looks well, but there's a problem when I need to add a collider to highlight an object.

    I can't add any 2D colliders since they can't be rotated in Z axis and are very imprecise in 3D. And some colliders have to be animated. I've tried some basic 3D colliders but they're way too inaccurate for some sprite shapes (and maybe it's a bug sometimes Raycast goes right through them without detecting).

    So what would you suggest, use mesh colliders or change the whole setup? I'm open to any idea at this point.

    PS: I've tried some ways to make it work without colliders, like checking if sprite bounds intersect with a Raycast, but couldn't quite make it work out.

    another PS: I should add that physics colliders work fine, it's when I need a trigger collider to interact with an object using the mouse cursor. Maybe there's some other way that I'm unaware of.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    If you're using Physics2D, it will be in the X/Y plane only, no tilting.

    That means the Camera should look mostly +Z, but slight Camera rotations may also suit you.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    This statement is entirely incorrect.

    There's nothing "imprecise" about 2D physics. The ONLY degree of rotational freedom is around the Z axis. 2D physics has 3 degrees of freedom; XY position and Z rotation.

    If you want other degrees of freedom then use 3D physics. There is no 2.5D physics. :)
     
    Kurt-Dekker likes this.
  4. DrVanillaCookie

    DrVanillaCookie

    Joined:
    Aug 23, 2021
    Posts:
    39
    You know, when I was setting the grid and the camera, I tilted the camera by 45 on X, other axis didn't rotate it the way I needed. Could that be the problem? But is there a way to tilt the camera differently? I remember trying lots of things, but either camera in editor was completely off, or Y wasn't vertical (I need it for gravity). My grid's cell swizzle is XZY if this makes a difference.

    I mean it's imprecise for what I currently have. Which is: sprites that are tilted by 45 on X and 2D colliders that doesn't tilt along with the object. And with perspective camera it's very imprecise. You're saying 2D physics has 3 degrees of freedom, but is there a way to tilt a 2D collider on Z axis? Am I missing something obvious?

    edit: nevermind, I got the rotation part, it's not what I'm looking for.

    My ideal scenario would be detecting objects by their sprites, but since I'm kind of a scrub at this, I couldn't make it work. I got to detecting if a ray goes through bounds of a sprite, but it looks like it detects transparent pixels as well. Then it got a bit too hardcore... I also heard you can achieve it with shaders somehow but I've no clue on this. So it looks like my best bet is to somehow make colliders work.
     
    Last edited: Aug 26, 2023
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    I said above what those three degrees of freedom were; XY position and Z rotation. By Tilt on the Z axis you mean tilt (rotate) on the X axis so no, that's not a degree of freedom it's got.

    If you rotate a collider around the X or Y axis then the collider is adjusted but projected onto the XY plane (it's still a XY plane, not rotated into 3D space because, it's 2D physics). You can see this in the Editor. If you use GetRayIntersection then you can perform a pseudo-3D raycast against the 2D XY plane. The 3D ray is projected against the XY plane and will work but don't expect a 3D collision normal from the hit.
     
  6. DrVanillaCookie

    DrVanillaCookie

    Joined:
    Aug 23, 2021
    Posts:
    39
    Yep, I figured the rotation part. Hey, since you're a freaky sun from Teletubbies maybe you know things.

    As I said before here's what I got: 3D worldspace with sprite objects, every object and the camera are tilted 45 on X. All 3D physical colliders work fine, since they don't have to be tilted. The problem is I need every object to be interactable on click and on mouse hover (and it should be quite accurate). Let's say there's a pine tree sprite (triangle shaped) and it's slightly swaying. Ideally its collider should be animated as well.

    What's the best approach here? Mess with 3D colliders (maybe mesh collider?) or somehow detect sprite with a ray? Or some other way? I've been dealing with this for over a week now, quite a headache.

    The reason I'm so obsessed with this is I've seen it work in Don't Starve. They have different engine and I'm not sure if they have the same camera setup (it looks like they do), but the mouse interaction with objects works quite well there.
     
  7. DrVanillaCookie

    DrVanillaCookie

    Joined:
    Aug 23, 2021
    Posts:
    39
    I figured I needed to tilt camera by 60, not 45. Which makes 2D colliders even more mismatched with the sprite. I'll be sticking with 3D colliders for now, but still I hope to find a better way.