I’m trying to make a simple physics - based building game where you build vehicles and machines and then try to beat obstacles with them, and I have this simple test script for a placement system:
local runservice = game:GetService("RunService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local gridSize = 2
local function Snap()
posX = math.floor(mouse.Hit.X / gridSize + 0.5) * gridSize
posY = math.floor(mouse.Hit.Y / gridSize + 0.5) * gridSize
posZ = math.floor(mouse.Hit.Z / gridSize + 0.5) * gridSize
return Vector3.new(posX, posY, posZ)
end
runservice.RenderStepped:Connect(function()
workspace.block_crystal.CFrame = CFrame.new( Snap() )
workspace.block_crystal.CanCollide = false
mouse.TargetFilter = workspace.block_crystal
end)
block_crystal is just a 2x2x2 cube, anchored.
The script will round the mouse’s position to the nearest 2 to align parts on a grid of 2 by 2 cubes.
However, when targeting the bottom, front and left (i think) the object goes inside the part you target:
robloxapp-20200514-1806056.wmv (2.4 MB)
I’ve looked things up before and I don’t really seem to understand any of them, I’ve looked at EgoMoose’s OOP placement but that uses a fixed canvas and I want to be able to place anywhere so long as snapped to a 2x2 grid.
I would also like to be able to account for object that are larger than 2x2x2 cubes, e.g. a 2x4x4 flat surface or a 1x2x2 tile, but my main priority is preventing them from going inside the targeted parts.
Can anyone help me stop the parts going inside when targeting the front etc. but also accounting for rotated parts as well? I want to be able to have custom models as well as blocks that might be rotated by 90 degrees.
Also, please don’t link another topic and not explain what to do because I can’t figure it out.
What do you mean? You want to check for collisions in the crystal you are placing, and if it is colliding then dont let them place it there? Or is it an issue with placement of the crystals?
Its an issue with placement, it goes inside the surface instead of connecting next to the surface
But I would also like to be able to check collisions, but that’s not important right now, placement is my priority
Try place it in the mouse.position but moving it by half the size.
I am currently working on a placement system. I use ray casting and i multiply the half of the part size by Vector3
of surface normal. This can solve your probleme
2 Likes
Maybe try this:
local runservice = game:GetService("RunService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local gridSize = 2
local function Snap()
posX = math.floor(mouse.Hit.p.X / gridSize + 0.5) * gridSize
posY = math.floor(mouse.Hit.p.Y / gridSize + 0.5) * gridSize
posZ = math.floor(mouse.Hit.p.Z / gridSize + 0.5) * gridSize
return Vector3.new(posX, posY, posZ)
end
runservice.RenderStepped:Connect(function()
workspace.block_crystal.CFrame = CFrame.new( Snap() )
workspace.block_crystal.CanCollide = false
mouse.TargetFilter = workspace.block_crystal
end)
TL;DR - I changed “mouse.Hit” to “mouse.Hit.p”
You could also try raycasting? I’m still confused of the issue tbh. I think raycasting may fix your issue tho
I’ve tried using hit.p and the same thing happens.
1 Like
To do the raycasting method, would I fire a ray towards the mouse hit and use the position returned? I may also have to set the position 0.1 stud closer to the player in case the same thing happens again
1 Like
Yeah, you fire a ray towards the mouse’s position from the camera’s position, that will detect it and roblox has system that automatically tells you what face it hit on the target, i can’t think of it rn tho so you may have to do your research.