How can you orient a bricks rotation to match that of the mouse direction?

I would like to orient a brick to match the direction a ray from a mouse is facing. The screenshot below might help show what I am looking to do:

However, in practice I’ve seen results like this:

xwd1nB16HF
https://streamable.com/6ckdxv

I’ve tried these snippets so far:

Mouse.Move:Connect(function()
  local MouseEnd = Mouse.Origin + Mouse.UnitRay.Direction
   local newRay = Ray.new(AddedVehicle.VehicleSeat.Position, MouseEnd.Position)
  ControlPart.Orientation = Vector3.new(newRay.Direction.Y, newRay.Direction.X, ControlPart.Orientation.Z)
end)
Mouse.Move:Connect(function()
                    local MouseEnd = Camera:ScreenPointToRay(Mouse.X, Mouse.Y)
                    local newRay = Ray.new(AddedVehicle.VehicleSeat.Position, (MouseEnd.Origin + MouseEnd.Direction))
                    ControlPart.Orientation = Vector3.new(newRay.Direction.Y, newRay.Direction.X, ControlPart.Orientation.Z)
                end)

I’ve tried a wide variety of things, each one not seeming to work. Effectively I’m just trying to make a plane that is controlled by the mouse. I at least believe there is a UnitRay involved for the mouse, but beyond that nothing seems to work.

Any help would be greatly appreciated!

What you’ll wanna do here is make use of mouse.Hit.p

You can just set the brick’s CFrame to CFrame.new(brick.Position, mouse.Hit.p)

2 Likes

This worked! For some reason I thought this functioned like Raycasting, in which Hit could be nil.

Thank you so much!

Don’t use Ray.new, it’s deprecated, instead use workspace:RayCast.

Where is Ray.new listed as depreciated. The API does not show this: Ray | Roblox Creator Documentation

I’m aware things like FindPartOnRay are depreciated, but it does not seem as though Ray.new is depreciated.

Thank you for correcting me, I forgot about that. Ray.new is not deprecated but functions that surround it are. Ray.new() and WorldRoot:Raycast() - #2 by OnlyJaycbee

1 Like