Optimizing the time it takes to do a series of linecasts?

Hi all.

With one of my tasks at the moment I need to check if an object is colliding with others of it’s kind when it’s first instantiated. This was easy to do, but a lot of these objects are instantiated in quick succession, making the requirement to wait a moment for the collision to register become an increasing hindrance the more objects are needed.
To work around this, I did a quick mock up of a linecast based approach. Each object has a series of empty GameObjects as children, placed at various corners. These are just used to easily fetch their location so that I can cast lines between them.
I have two arrays, a collisionCornersFrom array and a collisionCornersTo array, and as one might imagine I fill the arrays with the points I mentioned earlier.
I also set the object to a different layer and use a mask with the linecast so that it can’t hit the object we’re currently checking, then set it’s layer back to normal afterwards.

I use the following code to iterate through the arrays:

function CheckCollisions()
{
    var rayHit01 : RaycastHit;
    var numCorners = collisionCornersFrom.length;
    var collisionCheckTime = Time.realtimeSinceStartup;
    this.gameObject.layer = ignoreCheckLayer;
   
        for(var i=0; i < collisionCornersFrom.Length; i++)
        {
            var loopTime = Time.realtimeSinceStartup;
            if(Physics.Linecast(collisionCornersFrom[i].position, collisionCornersTo[i].position, rayHit01, collisionLayerMask))
            {
                generatorScript.isThisModuleColliding = true;
                if(generatorScript.debugMessagesOn){print(this.name + " has collided with " + rayHit01.collider.gameObject.name+" at "+Time.fixedTime);}
                collided = true;
            }
            print(this.name + " loop completed in " + (Time.realtimeSinceStartup - loopTime) + " seconds.");
        }
    print(this.name + " collision check completed in " + (Time.realtimeSinceStartup - collisionCheckTime) + " seconds.");
    this.gameObject.layer = moduleLayer;
}

This works without issue, but I can’t consistently get the time required to cast all the rays below 0.02 seconds, which is how long I need to wait for normal collision checks anyway, making the whole exercise unnecessary.
The length of the arrays vary between 8 and 16, with one notable exception being 32.

Is the time this function takes to complete about what I can expect for what I’m trying to do here, or does it take this long because of my crummy code?
If the latter, how should I go about optimizing or otherwise changing it?
If the former, do I have other avenues available for checking these collisions that don’t involve spherecast, as some of these objects can be too complicated for a spherecast to be accurate?

Thanks in advance for any help or advice offered.

The Debug.Logs are causing most of the time spent. Collect data first and then use Debug.Log only once, after everything is finished.

–Eric

1 Like

Hmm, I knew they caused a noticeable impact, but didn’t realize just how much.
I have the time it takes down to about 0.002 on average now, which is obviously far better and makes pursuing this actually viable, so thank you very much.
You always make me feel a little foolish, but in the best way possible. =)