Problem with dividing 2 by 2? (Or with raycasting or cframe?)

  1. I have a building system that’s based on rays (Ray.Position and Ray.Normal), but when i make a part with a Size.X = 2 then it just breaks and it’s position locks to 0, -340282346638528859811704183484516925440, 0.

  2. What is the issue? Include screenshots / videos if possible!
    image

  3. The only working solution i have is just making Size.X 2.001 instead of 2

The problem is on line 89

local Tool = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.TargetFilter = script.Parent.Parent
local debounce = 0.2
local prevobject = nil
local runservice = game:GetService("RunService")
local prevconnection
local camera = game.Workspace.CurrentCamera
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent.Parent, workspace.Bullets}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local enabled = true
local size = Vector3.new(1,1,1)
local color = Color3.new(1,1,1)
local material = "Plastic"
local shape = "Block"
local reflectance = 0
local transparency = 0

script.Parent.Activated:Connect(function()
	if canplace == true and enabled == true then
		enabled = false
		script.Parent.RemoteEvent:FireServer(ray.Position,ray.Normal,size,color,material,shape,transparency,reflectance)
		wait(debounce)
		enabled = true
	end
end)

script.Parent.Equipped:Connect(function()
	local char = script.Parent.Parent
	local plr = game.Players:GetPlayerFromCharacter(char)
	gui2 = script.Parent.ToolgunGui:Clone()
	gui2.Parent = plr.PlayerGui 
	rangevisual = Instance.new("Part")
	rangevisual.Size = size
	rangevisual.BrickColor = BrickColor.new("Lime green")
	rangevisual.Anchored = true
	rangevisual.CanCollide = false
	rangevisual.CanQuery = false
	rangevisual.CanTouch = false
	rangevisual.Material = Enum.Material.ForceField
	rangevisual.Transparency = 0.5
	rangevisual.Parent = game.Workspace.Bullets
	rangevisual.Shape = Enum.PartType.Block
	rangevisual.Name = "BuildVisual"
	for i,v in pairs(gui2.Frame.SizeButtons:GetChildren()) do
		v:GetPropertyChangedSignal("Text"):Connect(function()
			local sizex = tonumber(gui2.Frame.SizeButtons.X.Text) or 1
			local sizey = tonumber(gui2.Frame.SizeButtons.Y.Text) or 1
			local sizez = tonumber(gui2.Frame.SizeButtons.Z.Text) or 1
			--if sizex == 2 then
			--	sizex = 2.001 -- this makes it work normally
			--end
                        print(CFrame.new(ray.Position + ray.Normal * rangevisual.Size.X/2,ray.Position + ray.Normal) * CFrame.Angles(0,math.rad(90),0))
			size = Vector3.new(sizex,sizey,sizez)
			rangevisual.Size = size
		end)
	end
	for i,v in pairs(gui2.Frame.ColorButtons:GetChildren()) do
		v:GetPropertyChangedSignal("Text"):Connect(function()
			local colorR = tonumber(gui2.Frame.ColorButtons.ColorR.Text) or 255
			local colorG = tonumber(gui2.Frame.ColorButtons.ColorG.Text) or 255
			local colorB = tonumber(gui2.Frame.ColorButtons.ColorB.Text) or 255
			color = Color3.fromRGB(colorR,colorG,colorB)
			gui2.Frame.ColorPreview.BackgroundColor3 = color
		end)
	end
	for i,v in pairs(gui2.MaterialsFrame.Frame:GetChildren()) do
		if v:IsA("ImageButton") then
			v.MouseButton1Click:Connect(function()
				material = v.Name
				gui2.Frame.SelectedMaterial.Text = v.Name
			end)
		end
	end
	gui2.Frame.ReflectanceV:GetPropertyChangedSignal("Text"):Connect(function()
		reflectance = tonumber(gui2.Frame.ReflectanceV.Text) or 0
	end)
	gui2.Frame.TransparencyV:GetPropertyChangedSignal("Text"):Connect(function()
		transparency = tonumber(gui2.Frame.TransparencyV.Text) or 0
	end)
	gui2.Frame.Materials.MouseButton1Click:Connect(function()
		gui2.MaterialsFrame.Visible = not gui2.MaterialsFrame.Visible
	end)
	runservice.RenderStepped:Connect(function()
		if mouse.Target and mouse.Target.Parent then
			local angl = CFrame.new(camera.CFrame.Position,mouse.Hit.Position)
			ray = workspace:Raycast(camera.CFrame.Position,angl.LookVector * 50,raycastParams)
			if ray and ray.Instance then
				rangevisual.CFrame = CFrame.new(ray.Position + ray.Normal * rangevisual.Size.X/2,ray.Position + ray.Normal) * CFrame.Angles(0,math.rad(90),0)
				local mag = (char.HumanoidRootPart.Position - rangevisual.Position).Magnitude
				if mag < 4000 then
					canplace = true
					rangevisual.BrickColor = BrickColor.new("Lime green")
				else
					canplace = false
					rangevisual.BrickColor = BrickColor.new("Really red")
				end
			end
			if mouse.Target ~= prevobject then
				prevobject = mouse.Target
			end

		end
	end)
end)

script.Parent.Unequipped:Connect(function()
	--equipped = false
	if rangevisual then
		rangevisual:Destroy()
	end
	gui2:Destroy()
end)
1 Like

This seems like a Roblox bug. You divide the rangevisual part’s X length (2) by 2 (2/2 is 1) and multiply the normal with the quotient. Roblox decides to give you a garbage vector instead of just returning the normal. :+1:

See if replacing

ray.Normal * rangevisual.Size.X/2

with

if rangevisual.Size.X == 2 then ray.Normal else (ray.Normal * rangevisual.Size.X/2) 

stops this madness.

1 Like

Didn’t work, but still thanks for replying

	if ray and ray.Instance then print(ray.Position,'|', ray.Normal,'|', rangevisual.Size)
			rangevisual.CFrame = CFrame.new(ray.Position + ray.Normal * rangevisual.Size.X/2,ray.Position + ray.Normal) * CFrame.Angles(0,math.rad(90),0)

try adding this print statement

according to those prints, it looks like your doing this:
CFrame.new(VectorA, VectorA)
In other words, you are creating a cframe that has the same lookVector as it’s position, which isn’t possible.

ray.Position + ray.Normal * rangevisual.Size.X/2,ray.Position + ray.Normal

Since rangevisual.Size.X/2 is exactly 1, you are doing:

CFrame.new(ray.Position + ray.Normal,ray.Position + ray.Normal)
2 Likes

Ah i see now. Thank you for explaining, i thought it would just make the lookvector 0,0,0.
I fixed it by replacing

ray.Position + ray.Normal * rangevisual.Size.X/2,ray.Position + ray.Normal

with

ray.Position + ray.Normal * rangevisual.Size.X/2,ray.Position + ray.Normal * rangevisual.Size.X * 0.51

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