What do you want to achieve? Keep it simple and clear!
I want the tool to collide with other objects around it so it doesn’t clip inside them.
What is the issue? Include screenshots / videos if possible!
Half of the tool clipping through walls even though collisions are already set to true
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Have tried looking it up on the Dev Hub to no avail, as no posts fit my exact situation. I’ve also tried using AlignPosition, but I don’t know how it exactly works, so I might’ve done something wrong.
local function move()
if not isRotating then
local posX = math.floor(mouse.Hit.X)
local posZ = math.floor(mouse.Hit.Z)
local posY = mouse.Hit.Y
local orientationY = currentObject.PrimaryPart.Orientation.Y
debounce = true
local Tween = TS:Create(currentObject.PrimaryPart, TI,
{CFrame = CFrame.new(posX, posY + (currentObject.PrimaryPart.Size.Y / 2), posZ) * CFrame.Angles(0, math.rad(orientationY), 0)})
Tween:Play()
Tween.Completed:Wait()
debounce = false
end
end
Here’s the piece of code that moves the object to the mouse position. The code also creates sort of a hitbox around the tool.
don’t know if that’s the best solution but you could create raycasts shooting from every angle except up and down and add / subtract position by size like you did with posY
So I watched some videos on raycasting and got a basic sense of what it does, so I tweaked the code a bit to fix the errors it was outputting and put in some variables for better visualization and this is what I got:
And here’s the code:
local filter = {}
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Include
raycastParams.FilterDescendantsInstances = {
workspace.Map
}
local function move()
if not isRotating then
local posX = math.floor(mouse.Hit.X)
local posZ = math.floor(mouse.Hit.Z)
local posY = mouse.Hit.Y
local orientationY = currentObject.PrimaryPart.Orientation.Y
local halfWidth = currentObject.PrimaryPart.Size.X / 2
local direction = currentObject.PrimaryPart.CFrame.XVector + Vector3.new(halfWidth, 0, 0)
local rightRaycast = workspace:Raycast(
currentObject.PrimaryPart.Position,
direction,
raycastParams
)
if rightRaycast then
currentObject.PrimaryPart.Position += Vector3.new((rightRaycast.Distance * -1) - currentObject.PrimaryPart.Size.X/2, 0, 0)
end
debounce = true
local Tween = TS:Create(currentObject.PrimaryPart, TI,
{CFrame = CFrame.new(posX, posY + (currentObject.PrimaryPart.Size.Y / 2), posZ) * CFrame.Angles(0, math.rad(orientationY), 0)})
Tween:Play()
Tween.Completed:Wait()
debounce = false
end
end
Just so you know, another function is then played when I click that places the object according to the mouse’s position but I must’ve done something wrong.
don’t know if it would work but create a variable and set it to the desired position and instead of adding the vector like this: currentObject.PrimaryPart.Position += add it to the variable, and set the position to that variable at the end