Need help with offsetting my ray

So, I needed to offset my laser for detecting damage (it should follow the player slightly behind but when they stop moving it catches up) and it worked great! But then… I found out I couldn’t use .Touched on a moving part, so now I am detecting the results.Instance to see if I do damage or not, but I have an issue.

As you can see below, when I move the laser shows behind which is good but my actual ray wasn’t moved, therefore it detects humanoid when not touching, how should I go about fixing this?

https://gyazo.com/f08f48bb5af075bae28adfc0964b002e

(Client sided)

local function castRay(rTarget)

	--print("Target in castRay is set to"..rTarget)

	local laser_Thickness = .5

	local rayOriginPart = Eye
	local rayTargetPart = rTarget



	local origin = rayOriginPart.CFrame.Position
	local rayTarget = rayTargetPart.HumanoidRootPart.CFrame.Position
	local direction = (rayTarget - origin)

	results = workspace:Raycast(origin, direction, RayCastParameters)

	if not results then
		results = {Position = rayTarget}
	else
		if results.Instance.Parent:FindFirstChild("Humanoid") and not results.Instance:IsDescendantOf(workspace.HappyHome) and not results.Instance.Parent:IsA("Accessory") then
			print("Humanoid!")
		elseif results.Instance:IsDescendantOf(workspace.HappyHome) then
			print("This is a part!")
		end
	end

	local distance = (results.Position - origin).Magnitude
	
	local LaserPosition = LaserPart.Position
	tick()

	LaserPart.Size = Vector3.new(laser_Thickness,laser_Thickness,distance)
	
	if LaserPart.Position == Vector3.new(0, 0, 0) then
		LaserPart.CFrame = CFrame.new(origin, rayTarget - rTarget.Humanoid.MoveDirection * 2) * CFrame.new(0,0, -distance / 2)
	elseif LaserPosition ~= LaserPart.Position then
		LaserPart.CFrame = LaserPart.CFrame:Lerp(CFrame.new(origin, rayTarget) * CFrame.new(0,0, -distance / 2), 0.5)

		--[[local goal = {}
		goal.Position = CFrame.new(origin, rayTarget - rTarget.Humanoid.MoveDirection * 2) * CFrame.new(0,0, -distance / 2).Position
		local tweenInfo = TweenInfo.new()
		
		local tween = TweenService:Create(LaserPart, tweenInfo, goal)
		
		tween:Play()]]
	else
		LaserPart.CFrame = CFrame.new(origin, rayTarget - rTarget.Humanoid.MoveDirection * 2) * CFrame.new(0,0, -distance / 2)
	end
1 Like

You should lerp rayTarget,
Add a variable to store your previous position of rayTarget and a variable for tracking accuracy and use
local rayTarget = previousRayTarget:Lerp(NewTargetPosition, trackingAccuracy)

after you do that there would be no real need to offset the part itself


here is my result, if you want I can send over the place to you later

3 Likes

Woah! Thanks that is very very smart and solves all my issues, issue after issues have appeared, could you send the place? I want to check it out, and read it over to understand it more!

1 Like

Are you online? It would be great if you could send the place

1 Like

Sorry for late reply, here is the place file
Immafiremahlaser.rbxl (26.7 KB)

You did this all server sided? Impressive, when I did it it had a giant delay should I still handle visuals on client?

It will lag, i suggest rendering the laser on client but actually handling raycast on server so it appears smooth n all that

It seem pretty smooth, also if the laser is following the ray cast wouldn’t rending it on the client not make a difference?

1 Like