This is the correct position of the green square. Mouse.Hit returns a CFrame and the point it is returning is the absolute position (middle) of the part.
You offset the object based on its size, which depending on the axis its in, it should be PositionAxis + SizeAxis/2
If you are raycasting, you would use the Normal value to place the object, in which it would be: Position + Normal
(Though, you might need the help of CFrames to correct it.)
I’m not very experienced (and i’m coding this on a phone) so take it with a grain of salt, as it probably won’t work right away
Something you can do is to get the parts that are touching the red box with :GetTouchingParts and calculate the offset that the green box needs to be to not touch them. So the red box will be a ghost part, used justo to calculate the green’s part position
The code would be something like this
local PlayerDirection = Vector3.Zero
local BlockSize = Vector3.new (4,4,4)
local GhostBlock = Instance.new("Part")
GhostBlock.Parent = workspace
GhostBlock.Size = BlockSize
GhostBlock.Anchored = true
GhostBlock.Transparency = 0.9 -- turn to 1 when you're done debbuging
local Block = Instance.new("Part")
Block.Parent = workspace
Block.Size = BlockSize
Block.Anchored = true
while true do
if player.Character then
GhostBlock.Position = m.hit.Position
local Touching = GhostBlock:GetTouchingParts()
if Touching ~= nil then
local Offset = Vector3.Zero
PlayerDirection = (Player.Position - Block.Position).Unit
For i = 0, #Touching, 1 do
local GhostBlockDirection =
local Part = Touching[i]
local PSFP = Part.Position + (Part.Size / 2) * PlayerDirection
-- PSFP = Position of the Side Facing the Block
Offset += PSFP - GhostBlock.Position
end
Block.Position = Offset
end
end
task.wait()
end
(It will probably not work)
The I remembered
You could probably solve this with ShapeCasting
Try searching it out!
Yeah, while i was writting the code i realized it’s too complex for my 2 brain cells
You’ll need to figure out which axis to use (probably by checking on which axis the cube is not cointained in the part’s area).
Then get the distance from the cube center from the face in that axis.
Make the distance from the center to the face of the wall be half of the cube’s size.
Good luck if you try that, but shapecasting is probavly the better solution
Another hacky way is to create the ghost block and fire another raycast from the camera to the m.hit, then place the cube on the Result.Position
When it’s closer it can go between the ramp and the pillar, then it goes farther and get big, so it cannot go between the parts and get smaller, which repeats the problem.
I don’t know how to solve this, sry, never used shapecasting before ;(
But you can try raycasting first to get the ditstance and resize the object. Shapecast and resize again
Thank you so much! I just hope I can find a solution to this “final” problem.
I will then be able to concentrate on my game (already available in public currently but with a grab script using BodyPosition…)
I’m sending a message to ask for help again, “Shapecast collision” is a problem here because this brings the object back to the start (due to the change in size), which causes the object to flash…
Thanks!
Reminder: the object changes size because I am trying to reproduce superliminal
I was thinking, maybe if you do the following it’ll get better
Do a normal raycast to get the position and the distance
Resize the cube
Do the shapecasting
If the distance from the shapecast result is different from the raycast distance, then add a filter to ignore the shapecast instance (you can do this by changing the parent to another folder and ignore that folder on params)
Repeat the above process until the distances are equal
Once the shapecast is in place, once you move your camera, place the ignored part back to their original parent