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

Why does mouse.Hit.p give a different value when the camera is manipulated?

I made the following script to create rts style gameplay:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local char = player.Character
local cam = game.Workspace.CurrentCamera

function ShowPointer(camera, target)
	for i,v in pairs(game.Workspace.CurrentCamera:GetChildren()) do
		if v.Name == "Point" then v:Destroy() end
	end
	local point = Instance.new("Part", camera)
	point.Name = "Point"
	point.Size = Vector3.new(1,1,1)
	point.CanCollide = false
	point.Anchored = true
	point.Position = target + Vector3.new(0,1,0)
	point.BrickColor = BrickColor.new(21)
	local mesh = Instance.new("BlockMesh", point)
	mesh.Scale = Vector3.new(0.3,1,0.3)
	char.Humanoid.WalkToPoint = target
	local dist = (char.Torso.Position - point.Position).magnitude
	while point and dist > 4 and wait() do	
		dist = (char.Torso.Position - point.Position).magnitude
	end
	point:Destroy()
end

function OnButton2Up()
	ShowPointer(cam, mouse.Hit.p)
end

mouse.Button2Up:connect(OnButton2Up)

You can just copy and paste this script in a new project and it will work as it should, but whenever I add some camera manipulation, the mouse.Hit.p value messes up.

game:GetService("RunService").RenderStepped:connect(function()
	cam.CoordinateFrame = CFrame.new(char.Torso.Position) 
						* CFrame.new(0,30,10) 
						* CFrame.Angles(-20,0,0)
end)

Does anyone know how I can fix this?

1 answer

After some time of trial and error I found out that if you manipulate the camera then you will have to change its CameraType property to Scriptable.

game:GetService("RunService").RenderStepped:connect(function()
    cam.CameraType = "Scriptable" -- Sets the CameraType to Scriptable
    cam.CoordinateFrame = CFrame.new(char.Torso.Position) 
                        * CFrame.new(0,30,10) 
                        * CFrame.Angles(-20,0,0)
end)

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