How to get around this positioning issue?

Greetings,

I am trying to make a sort of projectile trajectory indicator, that would show the player how far the projectile will fly before disappearing. It is supposed to follow the mouse position. And i made it work with one critical issue, because of basic physics my indicator diverges from the mouse position. When my mouse is pointing into the sky, the indicator doesn’t quite follow it(even tho it does but because of math it doesn’t)

Here is how it looks https://streamable.com/kq75s7

Here is the script

local beam = Part:Clone()

beam.Parent = workspace

Mouse.TargetFilter = beam

game:GetService(“RunService”).RenderStepped:Connect(function()

  beam.CFrame = Player.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-.5)
  beam.CFrame = CFrame.new(beam.Position, Mouse.Hit.p)
  --wait (0.25)
  local dist = (beam.Position- Mouse.Hit.p).Magnitude

  local lenght = 75 

  if dist >= lenght then -
  	dist = lenght
  	
  end

  beam.Size = Vector3.new(1,1,dist)

  beam.CFrame = beam.CFrame * CFrame.new(0,0,-dist/2)

end)

Any advice would be greatly appreciated. Thank you for your time.

When you point the cursor in the air, it would be considered a nil value so you should probably use units or something like that i found in a community tutorial about recreating the lumber tycoon 2 hold system.

sry I forgot to add the video, take a look to get what i mean

Once you put your mouse in the sky the Mouse.Position would be infinite. What you gotta do instead is:

  1. Get the camera and mouse position.
  2. RealMousePos = (MousePos - CamPos).Unit * MaxStudLenght

could you please provide an example of how to implement this?

game:GetService(“RunService”).RenderStepped:Connect(function()

local RealMousePos = (Mouse.Hit.p - Camera.CFrame.Position).Unit * 75

beam.CFrame = Player.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-.5)
beam.CFrame = CFrame.new(beam.Position, RealMousePos)
–wait (0.25)
local dist = (beam.Position- RealMousePos).Magnitude

local lenght = 75

if dist >= lenght then
dist = lenght

end

beam.Size = Vector3.new(1,1,dist)

beam.CFrame = beam.CFrame * CFrame.new(0,0,-dist/2)
end)

this is how i did it and it only got worse

Use Mouse.Origin.p instead of the camera’s position, it is the origin point of the mouse starting at.

unfortunately that doesn’t solve the issue :frowning: darn this seemed like a simple thing to do now it’s really grinding my gears