Hi, I’m trying to make my models cframe match the part my mouse is pointing to, however, I am stuck with rotations. Anyone can help?
Video to show:
Code:
function MouseMoved()
if Item then -- If there is a Note selected
local MouseTarget = Mouse.Hit
local MouseObj = Mouse.Target
local TweenItem = Services.TweenService:Create(ItemCloned.PrimaryPart,
TweenInfo.new(.1), {Position = MouseTarget.Position})
TweenItem:Play()
end
end
So my code is supposed to look a little something like this?
function MouseMoved()
if Item then -- If there is a Note selected
local MouseTarget = Mouse.Hit
local MouseObj = Mouse.Target
local raycastResult = workspace:Raycast(ItemCloned.PrimaryPart.Position, MouseTarget.Position, RaycastParams.new())
if raycastResult then
local TweenItem = Services.TweenService:Create(ItemCloned.PrimaryPart, TweenInfo.new(.1), {CFrame = CFrame.new(MouseObj.Position, MouseObj.Position + raycastResult.Normal)})
TweenItem:Play()
end
end
end
local RayCastWhiteList = RaycastParams.new()
RayCastWhiteList.FilterType = Enum.RaycastFilterType.Blacklist
RayCastWhiteList.FilterDescendantsInstances = {Player.Character}
-- for your scenario you want every player and every note thats been placed,
--so you would want to also store your notes in a folder which
-- i'm assuming you already do (in workspace)
local Mouse = Player:GetMouse()
-- rayresult = raycast ( Origin , Direction * Distance , Params )
local rayResult = workspace:Raycast(HRP, (Mouse.Hit.p - HRP) * rayRANGE, RayParams)
if rayResult then
local hit = rayResult.Instance
local normal = rayResult.Normal
bullethole.CFrame = CFrame.new(Point, Point + normal)
With Mouse.TargetSurface you can calculate the normal where the mouse touches the part. Then with some math you can calculate a target CFrame for your animation. This is quite a bit smoother.
Remember that you still need to restrict which parts can touch or what happens if you point it at the sky. If this gets complicated you should use Raycast to calculate the normal.
function MouseMoved()
if Item then -- If there is a Note selected
local MouseTarget = Mouse.Hit
local MouseObj = Mouse.Target
local normal = MouseObj.CFrame:VectorToObjectSpace(Vector3.FromNormalId(Mouse.TargetSurface))
local newcframe = CFrame.lookAt(MouseTarget.Position, MouseTarget.Position + normal)
local TweenItem = TweenService:Create(ItemCloned.PrimaryPart,
TweenInfo.new(.1), {CFrame = newcframe})
TweenItem:Play()
end
end
Mouse.Move:Connect(MouseMoved)