Need an idea!

Hey! I am working on racing car game. My positioning system works like this: there are checkpoints along the track and script counts how much did you get. If you touch the trigger checkpoint + 1 happens. But! I don’t want to put all trigers for each car. How can I put 4 the same triggers for few cars? Is it even possible? Thanks in advance!

Perhaps some more details would aid others in assisting you. Why don’t you want to put all triggers for each car? The devil in in the details after all.

You got my idea :stuck_out_tongue: But let’s say I want 20 cars in a race. Kinda boring work, isn’t it?

It sounds like you are under the impression that you need to add a trigger collider per checkpoint per car?

I mean I add everything but every car need its own checkpoints and I want to put just 4 and every car will use it but let’s say first car hit it and it adds 1 to checkpoint for this car but this trigger still exists for other cars but not this one. I mean, is it even possible? :stuck_out_tongue:

I don’t think I’m following yet. Aren’t the checkpoints the same for each car?

Let me make it clear!

  1. There are 4 triggers along the track
  2. If car hit the trigger script adds 1 to checkpoint int
  3. But! Trigger must disappear after adding this point because if not, you wou’d be able to add as much points as you want.
  4. I’ve done all of these things but, let’s say there are 2 cars on the track. First car hits the trigger and the it dissappear. So the second car can’t enter it. What do I want to create is these 4 triggers for every car. NOT copying them for each of the cars! Car hit it, it dissappears for the first car but it still exists for the second.
    And now, is it possible to make? Or I have to put lots of triggers for every car :stuck_out_tongue:
    Thanks in advance!

Keep the triggers, but also keep a list for each car with the invalid triggers, meaning the ones it can’t get points from anymore. This way you can get away with a total of only 4 triggers for all cars. There is no need to have duplicate triggers for each car.

Create an array of booleans for each car.

When each car passes a checkpoint, set the corresponding bool to that checkpoint to true.

When all bools in the array are “true” then simply reset them.

1 Like

Thanks for this idea! Sounds legit :smile: Gonna test it right now! :slight_smile:

Or you could just keep a reference to the trigger since you already have those. No need to synchronize booleans with corresponding triggers.

1 Like

Could you please give me an example how to do it? Thanks in advance! :slight_smile:

You can use GetComponent() to get the colliders from your game objects. Or reference them directly via the inspector. There are lots of ways to get references to them.

I am still kinda confused :frowning: Can somebody write a little script or something? I just have no idea how to do it. I program in C#.

Still trying to figure it out and it still looks impossible for me :confused: Could somebody help me, please?

In the script where you handle the collisions (some kind of managing script that knows about all colliders and all cars), add something like this:

[SerializeField]
private Collider[ ] triggers;

Then add the game objects with the trigger colliders into that array. Then in code, you can just reference them via the array’s contents.

Thanks for the replay! But I am not sure, if this will work as it should. I mean, every car uses the same script which count’s checkpoints. Ok, I added this list with colliders and I can reference them but then still this object will disappear after first hit :<

And this script works like this: I added 4 checkpoints and few booleans which disables and enables objects. If you hit first checkpoint it will be disabled and then the second checkpoint is enabled. That creates a loop. But with more than 1 car it doesn’t work properly. And of course I can add many triggers for each car but I don’t want to do it because it complicates everything in my script.

Am I right? Or I missed something? Thanks for your time and help anyway! :smile:

That’s exactly what @Ian094 and I have been trying to point out: There is no need to disable the objects that have the colliders. If you do disable them, you run into the problem that other cars won’t “see” the colliders, so you need to leave them enabled. But of course, to prevent a car from getting multiple points from the same collider, you need to memorize that collider in a different way once a car has hit it.

One way to do it could be a script on every car that just collects the triggers it has run into. If a trigger is already in that list, you don’t give more points for that trigger. At the start of the track, you would then add a new “reset” trigger that will cause each car to reset its list.

Oh god, I think I finally understood this :smile: Thanks a lot! So, I will create a script with empty list for colliders. On trigger enter it will add a point and also a collider to the list. If the collider is on the list I don’t get more points than one and colliders reset after crossing the finish line. I think I got it :smile:

Exactly. In the end, you only need 4 + 1 triggers regardless of the number of cars.