Search Unity

Use Raycast, but ignore one axis entirely?

Discussion in 'Scripting' started by Fikule, Jan 10, 2018.

  1. Fikule

    Fikule

    Joined:
    Dec 25, 2014
    Posts:
    19
    Hi,

    I have an Orthographic game made in 3D. There is a mix of meshes and sprites. When Raycasting from the mouse, selection is nice and accurate.

    However, I would like to know how to cast a ray between two points in the game world and ignore the Z axis entirely.

    e.g.
    Player --- Floor --- Wall --- Target

    The player is a sprite, as is the wall. The floor is a mesh and the target is another floor tile. If I cast a full 3D ray between the Target and the player, the wall might not be hit on the Z axis. How can I tell the ray to ignore the Z axis so all objects between the player and target will be hit, regardless of their position on the Z axis?
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You can cast.. um...shapes? :) eg: capsule, sphere, box. Would that help?
     
  3. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    You can give the wall collider a box collider. That's what I would do because it's representing something in 3d space anyway.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Do a RayCast2D, and give everything Physics2D colliders.
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  6. Fikule

    Fikule

    Joined:
    Dec 25, 2014
    Posts:
    19
    The Physics2D colliders idea seems good for the situation. Does this then mean there's no way to do a 2D cast between both 3D and 2D colliders (since all it involves is ignoring one dimension)?

    I ask because I think adding a 2D collider to the floor might be difficult as it is a 3D hexagon and there's no specific collider for that (unless a 2D collider can shape itself around a 3D object on 2 specified axis?). The issue would be that since you select floor tiles the collision needs to be pretty accurate and the code does not store their real positions, just map positions. So I am relying on raycasting and Monobehaviour's MouseEnter/Exit methods for selection
     
    Last edited: Jan 10, 2018
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    If your game has all logic in a 2D plane, and is seen from the side, 2D colliders is the way to go. If it's got 3D elements, or is top-down, it's not ideal.

    Do you have a screenshot? That'd make it a lot easier to tell what you need.
     
  8. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I don't know what you application is, but 2.5 d is done in adventure games all the time and they use 3d colliders. The reason being the character will scale if he/she moves backwards into the room. They are generally using a slightly overhead viewpoint. If it was going to be straight on side view and the character never moved backward into the scene, it wouldn't matter.
     
  9. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Don't you mean it'll scale because it's rendered on a non orthographic camera? Colliders have nothing to do with scaling.
     
  10. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Technically you can manage this with a normal BoxCast / BoxCastNonAlloc (in fact, most of the cast functions would work). Just set the X and Y extents to something really small, like .1f, and the Z extent to float.PositiveInfinity, be careful to set the orientation to a version of the "facing direction" with the Z euler angle zeroed out, and the rest should be identical to the raycast version. I have no idea what the performance difference is between something like that and a typical raycast though.