Why does mouse.Hit.p give a different value when the camera is manipulated?
I made the following script to create rts style gameplay:
01 | local player = game.Players.LocalPlayer |
02 | local mouse = player:GetMouse() |
03 | local char = player.Character |
04 | local cam = game.Workspace.CurrentCamera |
06 | function ShowPointer(camera, target) |
07 | for i,v in pairs (game.Workspace.CurrentCamera:GetChildren()) do |
08 | if v.Name = = "Point" then v:Destroy() end |
10 | local point = Instance.new( "Part" , camera) |
12 | point.Size = Vector 3. new( 1 , 1 , 1 ) |
13 | point.CanCollide = false |
15 | point.Position = target + Vector 3. new( 0 , 1 , 0 ) |
16 | point.BrickColor = BrickColor.new( 21 ) |
17 | local mesh = Instance.new( "BlockMesh" , point) |
18 | mesh.Scale = Vector 3. new( 0.3 , 1 , 0.3 ) |
19 | char.Humanoid.WalkToPoint = target |
20 | local dist = (char.Torso.Position - point.Position).magnitude |
21 | while point and dist > 4 and wait() do |
22 | dist = (char.Torso.Position - point.Position).magnitude |
28 | ShowPointer(cam, mouse.Hit.p) |
31 | mouse.Button 2 Up:connect(OnButton 2 Up) |
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.
1 | game:GetService( "RunService" ).RenderStepped:connect( function () |
2 | cam.CoordinateFrame = CFrame.new(char.Torso.Position) |
4 | * CFrame.Angles(- 20 , 0 , 0 ) |
Does anyone know how I can fix this?