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