How to get mouse.hit.position but only a set distance away

local function castBeam()
	attachment0.Position = root.Position--+Vector3.new(0,1,0)
	attachment1.Position = mouse.Hit.Position
	local length = (root.Position-mouse.Hit.Position).Magnitude
	--print(length)
	if length > 60 then
		local percent = 60/length
		local newpos = mouse.Hit.Position:Lerp(root.Position,1-percent)
		attachment1.Position = newpos
	end
end

I’ve made a beam that goes from the players root part to their mouse and the length variable is used to then limit how long it can be so it doesn’t just go on for forever basically. The only problem is when the mouse position is far away in the background and the way i’ve setup to limit the length the beam ends up not reaching the top of the mouse which I don’t like. So I need a way to basically act as if the mouses hit position is only 60 units (or whatever distance i set) away instead of just cutting off the end of the line basically (which is essentially what im doing now).

Do you want the 60 units from the camera or the player’s character root?

Either way my solution would be to simply draw a vector between the hit.position and the reference point, and if the vector’s magnitude is greater than 60, I’d simply change it before using it to

local normalisedPosition = refPoint + vectorBetween.Unit * 60

Where vectorBetween is the vector from refPoint to the hit position, and refPoint is your character or camera or whatever.

Thanks for the reply. Here’s the modified code:

local function castBeam()
	attachment0.Position = root.Position--+Vector3.new(0,1,0)
	attachment1.Position = mouse.Hit.Position
	local length = (root.Position-mouse.Hit.Position).Magnitude
	--print(length)
	if length > 60 then
		--local percent = 60/length
		--local newpos = mouse.Hit.Position:Lerp(root.Position,1-percent)
		--attachment1.Position = newpos
		local normalPos = root.Position + mouse.Hit.Position.Unit * 60
		attachment1.Position = normalPos
	end
end

It seems to sort of work but it has the same issue as before where when in first person the beam does not quite reach the cursor and it also seems to be a little bit wonky when taking the mouse on and off objects in the distance.

local Player = game.Players.LocalPlayer

local mouse = Player:GetMouse()

local Camera = workspace.CurrentCamera

function RayCast(Position, Direction, MaxDistance, IgnoreList)

return game:GetService("Workspace"):FindPartOnRayWithIgnoreList(Ray.new(Position, Direction.unit * (MaxDistance or 999)), IgnoreList)

end

local function castBeam()

local MaxDistance = 60

local IgnoreList = {Player.Character}

local UnitRay = Camera:ScreenPointToRay(mouse.X, mouse.Y)

local Origin = UnitRay.Origin

local Direction = UnitRay.Direction

local HitPoint, instance = RayCast(Origin, Direction, MaxDistance, IgnoreList) -- Will goto max distance unless something is hit,

attachment0.Position = root.Position

attachment1.Position = HitPoint

end

This should work fine.

Nearly there. A couple bits of the vector math are just slightly off, possibly due to my explanation

local vector = mouse.Hit.Position - root.Position
local length = vector.Magnitude

A vector between two points is vector = to - from, and in this case we want to go to the mouse hit from the root. We need the vector separately as well as we need it later on:

local normalPos = root.Position + vector.Unit * 60

Needs to be based on the vector we calculated, not the world position.

Idk why it put it like that…

Thanks that code seems to work but it still doesn’t address the original issue I’m trying to tackle. I do appreciate it, but probably didn’t do a great job explaining the initial problem. I’m not trying to fix the functionality of the beam, I’m just trying to make sure it always reaches the tip of the players crosshair regardless of how long the beam is or if their crosshair.hit.position is 10k studs away. I can try to make a video showing exactly what’s happening if that would help, but basically if I aim really far away the beam stops before it reaches the crosshair.

I think videos would probably help, as I’m not sure what this beam looks like and whether the camera’s position needs to play into it or what.

If you can also sketch on top of a still frame screenshot roughly what it is you’re hoping it would look like, then hopefully that + video will be all the pieces needed to understand properly

Trying to implement your code I get the error “Vector3 expected, got Instance” on this particular line:


local function castBeam()
	local MaxDistance = 60
	local IgnoreList = {char,game.Workspace.mouseIgnore}
	local UnitRay = cam:ScreenPointToRay(mouse.X,mouse.Y)
	local Origin = UnitRay.Origin
	local Direction = UnitRay.Direction
	local HitPoint, instance = RayCast(Origin, Direction, MaxDistance, IgnoreList)
	attachment0.Position = root.Position
	attachment1.Position = HitPoint-----This is the line with the error
end

So I think the hitpoint variable cannot be applied to the attachments position.

And i made a mistake with the variables

local instance, HitPoint = RayCast(Origin, Direction, MaxDistance, IgnoreList)

You should be using

attachment.WorldPosition = HitPoint

for stuff like this.

For other information on Attachments…

1 Like

That makes sense thank you. Your solution seems to work perfectly as the laser always ends at the players mouse regardless of the distance and direction. If you don’t mind is the main difference just that you’re taking the 2d mouse position? because before the laser kept going way under the cursor.

Yes, it is take the X and Y axis of the mouse (2d Position) and turning it into a UnitRay (Vector3)