How to calculate distance between 2 parts

For my game, I have a spawning system where you click on an Item and then you will be able to spawn it(for example a vehicle).

I’m trying to calculate the distance needed for that model to be a few studs away from the spawn point no matter the size of the vehicle. I can’t just use 1 CFrame value for many different types of vehicles because they are each different sizes.

My main objective is for the vehicle to spawn 5 studs away from the spawn point no matter what size it is.

Any help is appreciated, thanks.

2 Likes

Could you show your spawning code? You most likely won’t have to use distance checking either.

Here is an example of the concept of it:

local spawnPoint1 = {
   CFrame.new(1,1,1)
}

vehiclespawn.OnServerEvent:Connect(function()
    vehicle:PivotTo(spawnPoint1[vehicle.Name]
end)

As you can see there is 1 CFrame value for where a specific vehicle will spawn, the issue is that this may work perfectly for 1 vehicle but for a different vehicles lets say one with a bigger size. The position of it will look completely off.

3 Likes

You’re using PivotTo, so you may want to move the pivot of the model to the bottom back center of the model for example. There’s a pivot editor tool in Studio, or you can position your model in the workspace exactly where it should be spawned, then use the properties pane or command bar to move the pivot point to the spawn point - in your example, to position 1,1,1 and orientation 0,0,0.

The distance between 2 parts can be calculated like this:

local distance = (part1.Position - part2.Position).Magnitude

This may work for 1 vehicle, but what about a vehicle double the size of it? would it look like it has been spawned at the same position?

Thats a different deal, the previous replier mentioned using the magnitude thing. In a situation like this you’d have to introduce size in this, if it is a model tho you might have to use raycasts somehow.

For every vehicle, you should set the pivotOffset to be the same relative spot on the vehicle. You can use that to spawn it 5 studs away no matter the size.

Oh yeah i forgot about those things, nice solution!

Do you mean I set the PivotOffset for all of the vehicles to the same value?

No, you should set the pivotOffset to the relative spawn-location of the vehicle. So then to spawn it, you’ll just do car:PivotTo(spawnCFrame). Then, because you set the pivotOffset, it’ll automatically maintain that offset.

My point is that I think you’re actually asking how to get a position for your model such that its edge is 5 studs away from your button, not as the title or most of the literal text of your post says. I’m sort of frustrated at all of the ChatGPT posters in this thread lightly skimming the first post and then posting irrelevant nonsense.

Yes, there’s other ways to go about this than the pivotoffset. You can get the bounding box of the vehicle and then offset their spawning position by half that size so that the corner or edge gets placed there, meaning if you have a button there, it will always be 5 studs away from it, for sure.

But about the pivotpoint technique. Let’s say the purple blocks represent a custom pivot point on these free model cars:

Well, if you were to have one spawning point, and you just used PivotTo to move all of these cars:
foo

The advantage of this one is that there’s less scripting and more manual tinkering and there’s no headaches if there’s some tiny part of the vehicle that extends the bounding box.
The downside is that you need to manually set this pivot point for every car.

edit: It’s this tool. It even snaps to the edges of the model’s bounds because that’s pretty much what everybody does with it most of the time

1 Like

I would go with a bounding box solution:

local originCFrame = CFrame.new(0, 0, 0) -- Bottom right CFrame of your vehicle when it spawns if orientated correctly

vehiclespawn.OnServerEvent:Connect(function()
	local orientation, size = vehicle:GetBoundingBox()
	vehicle:PivotTo(originCFrame + size / 2)
end)

This is assuming your Pivot is at the exact center of your vehicle.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.