Move an object correctly with mouse (ShapeCast)

image

In the picture, black corresponds to the wall and floor.

The red square is the position given by mouse.Hit with an object.
The green square is the desired position of the object.

The long red thread corresponds to the trajectory of Mouse.Hit

Blue corresponds to the player.

How to get the correct position (green square)? Thank you so much. .

I believe you can use Mouse.Hit.Position.

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.

Mouse.Hit.Position doesn’t work and gives me my red position. How do I get the green position?

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.)

Hello there!

I was trying to do it in a crazy way

Text i was writting

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!

Would it work correctly on walls at 45° or something else?

Yes I tested something like that which I didn’t succeed, I tried with:GetPartsInPart.

For shapecasting, this seems like a good idea but I don’t really know how to use it

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

Ok thank you very much for your help! I found this and I actually think it can be very useful!

2 Likes

I finally succeeded (still needs to be improved but it works!)

I only have one problem with my script, when I combine it with lines that change its size (for a Superliminal illusion) it happens that it oscillates

What I want:

Problem:

I think ot has to do with the changing size

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

Great you found a solution for yourself. Also hope you finish on recreating “superliminal” game on Roblox

Yes that’s what I do in the script, I just need to find a correction to this problem! Thanks!

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…)

shapecast paint
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

Hello again!

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

Hope it helps ;]

1 Like

The idea is interesting, but isn’t there a risk that the object will no longer take any collisions with obstacles?

I think that obstacles would still work, but only when you look directly at then.

But the only way to find out is testing
Hope i helped at least a little bit ;]