Issue with Raycasting coming from the world origin instead of the attachment

Hello,

I’ve been testing a couple of things before moving on to a game and trying to understand certain things better and I’m on Raycasting. My only problem is that for some odd reason the ray would come from the world origin instead if the attachment I put inside of the tool.


image

I even tried to copy a tutorial to see if I could maybe fix it and replicate it on to my script but even that causes the ray to come from the origin of the world instead of the attachment like I set. I don’t know if I’m missing something or if I just keep repeating the same mistake.

  • Script
local tool = script.Parent
local remoteFunction = script.RemoteFunction

tool.Activated:Connect(function()
	script.Parent.Fire:Play()

	local player = game.Players:FindFirstChild(tool.Parent.Name)
	local rayOrigin = tool.Handle.RayOrigin.Position
	local mousePos = remoteFunction:InvokeClient(player)
	
	local distance = (mousePos - rayOrigin).Unit * 500
	
	local rayCast = workspace:Raycast(rayOrigin, distance)
	
	if rayCast then
		print(rayCast.Instance)
	end
end)
  • Local Script
local remoteFunction = script.Parent.Script.RemoteFunction
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

remoteFunction.OnClientInvoke = function()
	return mouse.Hit.Position
end
1 Like

Can you show how your tool is set up and maybe visualize where the rays are going?

2 Likes

Is this what you mean?
image

1 Like

Attachment has two position properties, Position and WorldPosition
Position is the position of the attachment relative to that of its parent
WorldPosition is the position of the attachment relative to the world (workspace)
So you should be using the Attachment’s WorldPosition instead. Example
Lets say the world pos is Vector3.new(17,4,23) but the position would be Vector3.new(.1,0,.1)as the attachment is prob that close to the handle. Since the ray is firing from World Coordinates, the script is thinking Vector3.new(.1,0,.1) is the world position it needs to fire from, which is not the right position.

To sum up, use tool.Handle.RayOrigin.WorldPosition instead of tool.Handle.RayOrigin.Position

3 Likes

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