Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[Solved] How can I make my car able to collide with other cars?

Asked by 3 years ago
Edited 3 years ago

So I am working on a car racing game, but the parts of the car must have CanCollide set to false in order for the car to move, except for the wheels. The problem is that since the wheels are able to collide, if the cars crash into each other, they get stuck inside of each other. I could just make it so they can't crash into each other at all, but where's the fun in that? I want the cars to be able to crash into each other without getting stuck inside one another, but the only way I can think of doing this is by making them able to collide whenever they touch something. I'm not too sure how to do this though, considering that I can't use collision groups because both colliding objects have CanCollide set to false. Does anyone know a way i might be able to do this? Your help would be greatly appreciated :)

2 answers

Log in to vote
1
Answered by
Rinpix 639 Moderation Voter
3 years ago

If I understand what you're saying, you don't want the body of the car colliding with its own wheels, but you do want it colliding with the bodies of other cars. You also don't want the wheels of one car colliding with the wheels of another. And I'd hazard a guess that you want the wheels colliding with the game environment. Otherwise you'd have a car that just falls through the map.

I'd just create two collision groups, in addition to the default one(put something like this in a server script in ServerScriptService, or just use the collision groups panel in Studio to manually set it up): Set CanCollide to true on all of the parts first, though.

local physicsService = game:GetService("PhysicsService")
local partsGroup = "Parts"
local wheelsGroup = "Wheels"

physicsService:Create(wheelsGroup)
physicsService:Create(partsGroup)

physicsService:CollisionGroupSetCollidable(partsGroup, wheelsGroup, false)
physicsService:CollisionGroupSetCollidable(wheelsGroup, wheelsGroup, false)

If you meant you don't want the bodies of cars colliding with the game environment, you could just do this:

physicsService:CollisionGroupSetCollidable(partsGroup, "Default", false)

That way, everything in the game environment won't collide with the parts of the car's body.

And whenever you spawn a new car, put something like this inside of a server script inside of the car:

local physicsService = game:GetService("PhysicsService")
local partsGroup = "Parts"
local wheelsGroup = "Wheels"

local parts = car.Parts
local wheels = car.Wheels

for i, v in pairs(parts:GetChildren()) do
    physicsService:SetPartCollisionGroup(v, partsGroup)
end
for i, v in pairs(parts:GetChildren()) do
    physicsService:SetPartCollisionGroup(v, wheelsGroup)
end

Or you could just assign the parts to their collision groups beforehand, because whenever the car is cloned, the collision groups to which its parts belong is carried over.

0
This was really helpful, but if I set CanCollide to true on the parts of the car, it won't be able to move. I'll definitely be using CollisionGroups later on though, thanks for showing me how :) ABHax101 4 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

Hello! I just solved this problem by myself. I added a part to the car body called 'hitbox' and added a collisiongroup that let the hitboxes collide with other hitboxes and nothing else, so the car still acts the same way.

Answer this question