Raycasting out from a cuboid?

So I’m following a pretty good tutorial on YouTube, which goes through how to make a 2D character controller. However I’m making a 3D controller, so I need to apply 3D space where appropriate.

Right now I did a sketch of the cuboid (in the tutorial it’s just a quad), and written down the local unit coordinates of each corner point of the cuboid.

Here’s a quick representation I just made in MS Paint:
3144158--238693--upload_2017-7-14_2-20-57.png
All those "0.5"s and are the unit positions of the cuboid’s corners. And currently the way I see it, I will have to cast out 3 rays from each point, to detect collision from all possible sides.

Like so:
3144158--238694--upload_2017-7-14_2-27-31.png

Here is why I wrote this thread:

A cuboid has 8 corners, which means there will be 24 rays being cast outwards from this single cuboid. And there will be a lot of these. And that’s my concern.

How efficient would this be, in terms of processing power? I know raycasts are rather cheap, but let’s say I have 10 of these cuboids being rendered at the same time. That means there will be 240 rays, and this will just multiple, the more cuboids there are on-screen.

Would this be a problem? And if so, what would be a better way of doing this (detecting collision through casts)?

Now keep in mind, although this may not be a problem processing-wise (at least for a single cuboid). I still need to write some code for every single raycast, and what happens if it hits something. As well as what happens if all casts from a certain direction don’t hit anything (character falling). This means well over 24 lines of very similar (but different) code, just for handing collision. Which I can already imagine would be a nightmare to maintain if one of those raycasts wasn’t working properly.

Just imagine. 24 raycasts, all with very similar coordinates, all with similar casting directions and lengths, all resulting in similar results if they hit something, and one of them doesn’t work and you need to find out which one.

Now before someone says this belongs in the Physics section. This isn’t talking about the actual physics that the raycasts will achieve, but whether using and coding these raycasts is efficient.

Only thing I can think of, would be a boxcast, but I don’t exactly know how I would handle detection from specific sides and how to detect if none of the corners of a certain side hit anything.

Add component > Box collider

No, using raycasts for this isn’t efficient at all. You would also need to raycast from the middle of edges, 2 for each edge. Say a sphere collider with radius 0.4 travels towards your cuboid, the raycasts wont pick it up if it hits it directly in the middle of an edge.

Then there’s the problem of small or sharp colliders colliding with the face, that wont be detected either. Raycasts weren’t built for this purpose. A convex mesh is so much easier to compute physics for because the physics engine is doing simple math on tetrahedrons (middle of the mesh to each triangle).

Another problem is two cubes wont detect each other unless they have colliders attached. If you have colliders on them though, what’s the point of using raycasts?

1 Like

Well I was planning on making my own controller, instead of using RigidBody or CharacterController, mainly so I’d have more control over how the physics work. And collides don’t exactly work without them. So the first thing I thought of for detecting stuff, is casts.

Not sure what you’re trying to do here but if you just wanna do 240 raycasts per frame, that might work… I guess it depends on how dense your environment is and whether the raycasts will be able to efficiently discard vast quantities of objects based on failed AABB testing, which I presume the Unity physics engine does to keep down its workload.

Now if you’re doing it on mobile that’s gonna be a lot less performant than a desktop, so keep that in mind.

You already know which one hit: it’s the one you are doing. Computers do one thing at a time. This is no different.

Another way to perhaps achieve what you want is to put a box collider on these things and then when you get a collision, look at the .point property of the collision to see which corner, if it was a corner that you hit.

That’s not necessary. With such a simple geometry you can procedurally develop expressions for all 24 of the raycasts. Here, I took a stab at it and this is a script you can stick on any cube gameobject and see the rays in real time in the editor. It takes into account position and rotation of the cube, but NOT the scale.

using UnityEngine;
using System.Collections;

// stick this on a standard Unity3D cube and see the corner rays pop out
public class DrawCornerRays : MonoBehaviour
{
    // these are the six outward facings of a cube (not including Z-rotational identities)
    Quaternion[] facings = new Quaternion[] {
        Quaternion.identity,
        Quaternion.Euler( 0, 90, 0),
        Quaternion.Euler( 0, -90, 0),
        Quaternion.Euler( 0, 180, 0),
        Quaternion.Euler( 90, 0, 0),
        Quaternion.Euler( -90, 0, 0),
    };

    const float CubeSize = 0.5f;

    void OnDrawGizmos ()
    {
        Gizmos.color = Color.cyan;
        foreach( var rotation in facings)
        {
            for (int corner = 0; corner < 4; corner++)
            {
                // produce the "forward" four corners with a
                // little old school bit flicking
                Vector3 position = new Vector3(
                    (corner & 1) * 2 - 1,
                    (corner & 2) - 1, 1) * CubeSize;

                // move to our cube center
                position += transform.position;

                // rotate the rays by the cube's rotation
                Quaternion actualRotation = transform.rotation * rotation;

                // transmog them by the facing we're dealing with for this face
                position = actualRotation * position;

                Ray ray = new Ray( position, actualRotation * Vector3.forward);

                Gizmos.DrawRay (ray);
            }
        }
    }
}
1 Like

Just do one raycast and say fuck it to clipping, fly by the seat of your pants man

*scuse me’ french

But better advice, just use a physics object constrained in some dimensions to do what you’re describing.

1 Like