Need help figuring out how to send mouse rotation to part rotation


I’m trying to get my tool to shoot a hook into wherever the mouse is pointed, and have the hook “face whatever direction the mouse was shot at”

Issue

  • The hook won’t change orientation/rotation to face the desired position
    https://files.catbox.moe/5voaex.mp4
    The hook is always facing a set direction no matter where you shoot as you can see

What i’ve tried

  • Mouse Look Vector
  • Mouse UnitRay
  • Various other Random Rotation functions
	local Rotation = mouse.Hit.LookVector
    local unitRay = mouse.UnitRay
    local direction = unitRay.Direction

They all didn’t not work as intended

Code

The tool used consists of 2 scripts
Below are portions or main parts of the scripts

Local Script

tool.Activated:Connect(function()
	local targetPosition = mouse.Hit.Position
	local initalPos = hookReference.Position 
	local distance = (targetPosition - initalPos).Magnitude
	local face = mouse.TargetSurface
	local Rotation = mouse.Hit.LookVector

	if distance <= minDist then
		--	print("too close")
	elseif distance <= maxDist then
		print()
		event:FireServer(targetPosition, hook, Rotation)

	elseif distance >= maxDist then
		--	print("out of range")
	end
end)

ServerScript (Inside a RemoteEvent inside of the Local Script)

local fireEvent = script.Parent.Parent.event


local function Fire(player, pos, hook)
--	print(pos)
	
	hook.Position = pos
	hook.Anchored = true
	hook.WeldConstraint.Enabled = false
end


fireEvent.OnServerEvent:Connect(Fire)

note

Any help on how I can achieve my desired effect would be helpful, I’ve been looking through a lot of the Roblox Documentation for help but I couldn’t find anything that could work, If you know how I can fix this or where I can look to find how to fix it, that would be appreciated .

send a CFrame instead of position and rotation. CFrames include both of them

this is how you create a cframe at a position facing another position:

local CFrame = CFrame.lookAt(initialPos, targetPosition)

If I understand correctly and implemented it right this is what it should somewhat look?

Local

tool.Activated:Connect(function()
	local targetPosition = mouse.Hit.Position
	local initialPos = hookReference.Position 
	local face = mouse.TargetSurface
	local Rotation = mouse.Hit.LookVector
	local distance = (targetPosition - initialPos).Magnitude
	
	if distance <= minDist then
		-- print("too close")
	elseif distance <= maxDist then
		local SendCFrame = CFrame.new(initialPos, targetPosition)
		event:FireServer(SendCFrame, hook)
	elseif distance >= maxDist then
		-- print("out of range")
	end
end)

Server Script

local function Fire(player, pos, hook)
--	print(pos)
	
	hook.CFrame = pos
	hook.WeldConstraint.Enabled = false
	hook.Anchored = true

end

cause all that happened from this was that the hook slightly changed rotation and stayed at the base of the gun

so if what i’ve understood about the CFrame LookAt is that
the first Vector3 argument is the position of the CFrame itself.
and he second Vector3 argument is the point that the CFrame should be looking at.
I did some playing around with it and it anyway I interpret this doesnt work how intended
So Is it the way i’m doing it wrong or am I not going about it correctly?

	local Rotation = mouse.Hit.LookVector

should be

       local direction = CFrame.new(initialPos):lookAt(targetPosition).lookVector

Don’t know if it’ll work, just pulled it from my head

Try setting the hook’s CFrame to CFrame.lookAt.

--Local Script
tool.Activated:Connect(function()
	local targetPosition = mouse.Hit.Position
	local initalPos = hookReference.Position 
	local distance = (targetPosition - initalPos).Magnitude
	
	if distance <= minDist then
		--	print("too close")
	elseif distance <= maxDist then
		event:FireServer(CFrame.lookAt(targetPosition, initalPos) * CFrame.Angles(0, math.rad(180), 0), hook)
	elseif distance >= maxDist then
		--	print("out of range")
	end
end)

--Server Script
local fireEvent = script.Parent.Parent.event


local function Fire(player, CF, hook)
	hook.CFrame = CF
	hook.Anchored = true
	hook.WeldConstraint.Enabled = false
end


fireEvent.OnServerEvent:Connect(Fire)

That won’t work since CFrame.new returns a CFrame object, not a Vector3 object. The lookAt function returns a Vector3 object, but you can’t chain the lookVector property to the result of CFrame.new.

They should use lookAt probably, that works 90% of the time

After some minor adjustments to the code it actually “somewhat” worked.
Other than it randomly flinging me on the first fire it seems to work

script ended up being

tool.Activated:Connect(function()
	local targetPosition = mouse.Hit.Position
	local initalPos = hookReference.Position 
	local distance = (targetPosition - initalPos).Magnitude

	if distance <= minDist then
		--	print("too close")
	elseif distance <= maxDist then
		event:FireServer(CFrame.lookAt(targetPosition, initalPos) * CFrame.Angles( math.rad(90), 0, 0), hook)
	elseif distance >= maxDist then
		--	print("out of range")
	end
end)

your initial code seemed to look like every other attempt but after changing the CFrame.Angles it fixed

only difference is that your initial change in the angle was that Y was set to 180 which made it stand up straight like all the other times, but moving it to the X spot and switching it to 90 to factor in the resting position of the hook made it actually face the right way

		event:FireServer(CFrame.lookAt(targetPosition, initalPos) * CFrame.Angles( math.rad(90), 0, 0), hook)

Thinking back I probably could’ve already had the solution I just needed to switch the XYZ cords around lol
thanks for helping :heart:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.