Identifying a trigger without strings?

I’m building a checkpoint system for racing, and when the car hits a checkpoint I need to identify exactly which marker it has hit…

My general idea was to use OnTriggerEnter, but how do I extract what that is without comparing strings or sending messages? Tag? Layer?

I have considered layer but there’s only a finite number of those available to me…

Due to ‘String Interning’, I believe I won’t be having such a bad issue with this :slight_smile:

I have a system based on waypoints, and instead of use any collision trigger, I’m simply monitoring the entity location upon the waypoint location, if its near of (myDistance, let’s say 0.2) , then I will execute some code or advance to the next waypoint.
As all the waypoints are part of an array, so it’s easy to know exactly wich waypoint I’m “hitting”

Well, aside from the player vehicle being capable and willing to exploit any lap/position counter, the question is which is faster…

4 car distance calculations against say, 16 checkpoint markers (polling every say, .25 of a second?) vs 16 trigger colliders, and each car simply checks a cached string value?

Summary, any given check will be performing 64 Vector2.Distance (or vector 3 if you have a very funky shaped track).

However, you would really only need to check for ‘the next waypoint’, since you’re obviously not going to increment if they drive backward. So that would be 4 distance checks per calc, vs 4x if (collider.object.name == “blah”)…

It does sound like a trivial amount of computing either way

In theory any string operation would be slower than a vector2.distance or vector3.distance or whatever operation of this type

You could also have a Checkpoint script with public int checkpointId, but I’m not sure whether GetComponent is faster than a string comparison. In any case, I would assume you don’t hit a checkpoint so often that it would actually make a difference.

Use triggers with OnTriggerEnter is my suggestion.

Or you could implement the distance between a point and a line equation and run checks but I without testing who knows which offers better performance. This has the benefit of allowing you to only run a check against the next way point instead of them all…

Of course you could always enable triggers when the one before is triggered, and disable triggers after the last car passes through…

Tons of different ways to do it and the performance differences would probably be negligible.

Triggers are probably simpler for most tasks and also pretty efficient. If you do decide to try out the distance checking approach then one thing you should definitely avoid is Vector3.Distance. It is much faster to use the squared distance for comparisons:-

var sqDist = (pt1 - pt2).sqrMagnitude;

Getting the squared distance is a simple bit of arithmetic, but the slow square root operation must be applied to this to get the actual distance. You will notice the difference if you are doing 64 distance checks per frame :wink: