Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is mouse.hit inaccurate?

Mouse.hit seems to be very inaccurate when it comes to firing objects at the mouses position. Here is the snippet of my code that deals with firing the object.

As you can see below, I am firing a part at the mouses lookVector, as if I were to aim it at the mouse.hit.position, the attack would fall down on me if i were to fire it straight up

local function fire(object, direct)
	
	if object ~= nil then
		local bodyvelocity = Instance.new("BodyVelocity")
		bodyvelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		object.CFrame = CFrame.new(object.Position, player:GetMouse().hit.p)
		bodyvelocity.Velocity = player:GetMouse().hit.lookVector * 50
		object.Anchored = false
		bodyvelocity.Parent = object
		game:GetService("Debris"):AddItem(object, 3)
	end
end

Here is a gif to explain my problem https://gyazo.com/33fdf3e0480206f39475827996972040

^not sure why it keeps blocking it ._.

1 answer

✓ Accepted answer

Its because you are setting the lookVector of mouse.hit as the velocity

you need a unit vector pointing to mouse.hit's position from the fireball (think of it as an arrow with the lenght of "1" pointing at the direction of mouse.hit from the fireball, if applied to a bodyVelocity, it will make the object go where the arrow is pointing)

you can achieve it with this line of code

bodyvelocity.Velocity =  (player:GetMouse().hit - object.Position).unit * 50

This discussion is preserved as it was. New answers, comments, and votes are closed.