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

Collision detection with placement system?

Asked by
zblox164 531 Moderation Voter
6 years ago

So I made a very simple placement system that literally just places objects along a 2x2 grid. No rotation or collision. My problem is I want to detect collision between objects and then set a variable so you can't place the object. My localScript is in a GUI in StarterGui. I tried to detect collision with the Touched and TouchEnded events but that did not work. I tried those events in a Script and in the localScript I am using for placement. Here is my current code: Note > I am just placing a single part that is not even in a model.

-- Player Variables
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

-- Position Variables
local PosX
local PosY = 2.1
local PosZ

-- Object Variables
local Gui = script.Parent
local Button = Gui.TextButton
local Baseplate = workspace.Baseplate
local Texture = Baseplate.Texture
local Model -- This is going to be a part

-- Textures
local GridId = "rbxassetid://13786085"

-- Snap to grid Variables
local Grid =  2
local Snap = 2
local Rounding = 0.5

-- Debounce
local IsPlacing = nil

-- functions

-- Snaps the part to a grid
local function Round()
    PosX = math.floor(Mouse.Hit.X / Snap + Rounding) * Grid
    PosZ = math.floor(Mouse.Hit.Z / Snap + Rounding) * Grid
end

-- Moves the part
local function Movement()
    if IsPlacing == true and Model:IsA("Part") then
        Round()
        Model.Position = Vector3.new(PosX, PosY, PosZ)
    end
end

-- Places the object
local function Placement()  
    if IsPlacing == true and Model:IsA("Part") then
        local FinishedModel = Model:Clone()
        FinishedModel.Parent = workspace.Clones
        FinishedModel.CanCollide = true

        Model:Destroy()
        IsPlacing = false
        Texture.Texture = ""
    end
end

-- Starts the placement
local function StartPlacement()
    Model = game.ReplicatedStorage.Movement:Clone()
    Model.Parent = workspace
    Model.CanCollide = false
    Texture.Texture = GridId
    IsPlacing = true

    Mouse.Move:Connect(Movement)
end

Button.MouseButton1Click:Connect(StartPlacement)
Mouse.Button1Down:Connect(Placement)

Thanks for the help!

1 answer

Log in to vote
4
Answered by 6 years ago

Whenever I make placement systems, I like to use Region3s for collision detection. I use Region3.new() and use opposite corners in the model to create a Region3 that has the size of the model.

I check if the region3 is overlapping anything other than the model and if it is, the model is colliding with something. However, if it is not, then the model is not colliding with anything.

Here is some information on Region3s.

0
Thanks I will check that out! zblox164 531 — 6y
0
No problem Operation_Meme 890 — 6y
Ad

Answer this question