Stopping Touched Events From Firing With Wrong Collision Group

I’m making a car game, and one of the features is that during a race, because physics replication on cars is a bit unreliable and likely to fling you off the track if you touch a competitor, opposing cars are made can collide off and set to a dummy collision group that collides with nothing.

Another features is that some parts of the players car will have .Touched events on them for various functions, such as effects when colliding with scenery.

Now the problem arises when you try and drive through a competing car with their collisions off, as .Touched does not respect collision groups and so will fire for every single part in the opponents car, for every touched event in your car, every frame. And even without any code actually in the connected function, this hogs your entire CPU and drops the frame to nothing.

Is there any trickery I can perform to stop this happening while retaining can-collide of cars and collisions effects, without adding touched events to every piece of scenery in the game?

1 Like

Before I start, I’d like to tell you my ideas aren’t the best…

But in theory, whenever your car gets generated, how about making a stringvalue and putting it under every parts of the car(or at least the exterior)? You can assign a value to it such as “Player1”, and whenever they collide, you can perform a simple check

Lets say that the part that a player touched was name “hit” and the value was named “check”

if hit:FindFirstChild("check") then
    if check.Value == thepersonsname then
        --action

well this is again, in theory

Thank you, but the problem is that the act of having the touched events connected causes too much CPU drain, nothing inside the touched callback function will help that

1 Like

I can think of a way that should be less intensive, but I don’t know if it will work. This method would use some raycasting, racasting from the car in different directions.

Now maybe you think that we are raycasting every frame, but I’ve considered that and thought of a different method:

  1. You first raycast in 8 different directions, each with a ray length of let’s say, CarSpeed * 2, which basically means the faster the car the longer the ray, you are going to do this one time per second
  2. If let’s say, the ray hits another car, activate a function the raycasts at a faster rate but a shorter distance, so, you would raycast at 5 times per second and the length would be CarSpeed
  3. Keep on doing number 2 for one second in a loop, and break it when the loop ends or it hits something
  4. If that ray hits something, you would cast a ray at 10 times a second at a length of CarSpeed / 5, and when that ray hits something, activate the touched event, if it doesn’t hit anything, go back to step 2, and if that doesn’t hit anything, go back to step 1

For visuals:

local CarSpeed = 10

local Step1
local Step2
local Step3

Step1 = function()
	local RayParts = -- Create 8 rays then get the parts
	if RayParts then
		return Step2()
	end
end

Step2 = function()
	for i = 1, 5 do
		wait(0.2)
		local RayParts = -- Create 8 rays then get the parts
		if RayParts then
			local Touched = Step3()
			if Touched then
				return Touched
			end
		end
	end
end

Step3 = function()
	for i = 1, 10 do
		wait(0.1)
		local RayParts = -- Create 8 rays then get the parts
		if RayParts then return true end
	end
	Step2()
end

while true do
	local Touched = Step1()
	if Touched then
		if CollisionGroup then
			-- Do stuff
		end
	end
end

Set Workspace.TouchesUseCollisionGroups to true

2 Likes