Using Ray To Find Points Sometimes WAAAAAAAAY Off

Hello,

The topic may seem exaggerated but I’m lost in this code at the moment. I have created a segment of code that handles manipulating a player and their target in a combat sequence to push both players up at an angle then send the target back down at an angle. Here is the code created to handle this effect:

local ORIGIN = Char.HumanoidRootPart.CFrame
local TARGETA = ORIGIN*CFrame.new(0,20,-30)
local TARGETB = ORIGIN*CFrame.new(0,22,-28)
local TARGETC = ORIGIN*CFrame.new(0,0,-60)
local IgnoreList = {Char,HRP.Parent}
local RAYA = Ray.new(ORIGIN.p,TARGETA.p)
local HITA,HITPOSA = workspace:FindPartOnRayWithIgnoreList(RAYA,IgnoreList)
if HITA then
	TARGETA = CFrame.new(HITPOSA,HITPOSA+CFrame.new(RAYA.Origin,RAYA.Direction).LookVector)
end
local RAYB = Ray.new(ORIGIN.p,TARGETB.p)
local HITB,HITPOSB = workspace:FindPartOnRayWithIgnoreList(RAYB,IgnoreList)
if HITB then
	TARGETB = CFrame.new(HITPOSB,HITPOSB+CFrame.new(RAYB.Origin,RAYB.Direction).LookVector)
end
for i=0,1,.1 do
	wait()
	HRP.CFrame = HRP.CFrame:Lerp(TARGETA,i)
end
HRP.Anchored = true
for i=0,1,.1 do
	wait()
	Char.HumanoidRootPart.CFrame = Char.HumanoidRootPart.CFrame:Lerp(TARGETB,i) --TARGET2??
end
Char.HumanoidRootPart.Anchored = true
FightA5Player:Play()
Char.Head.HPunchSE:Stop()
wait()
Char.Head.HPunchSE:Play()
local RAYC = Ray.new(TARGETB.p,TARGETC.p)
local HITC,HITPOSC = workspace:FindPartOnRayWithIgnoreList(RAYC,IgnoreList)
if HITC then
	TARGETC = CFrame.new(HITPOSC,HITPOSC+CFrame.new(RAYC.Origin,RAYC.Direction).LookVector)
end
Human:TakeDamage(8)
FXLOAD()
HRP.Anchored = false
Char.HumanoidRootPart.Anchored = false
for i=0,1,.1 do
	wait()
	HRP.CFrame = HRP.CFrame:Lerp(TARGETC,i)
end
Human:TakeDamage(4)

In this code, the two identified objects is the using player’s character (Char) and the target player’s character (HRP.Parent). ORIGIN is the using player’s character, TARGETA is the destination prior to casting a ray to define a point not obstructed, TARGETB does the same for the path of the character, and TARGETC is the point land the enemy character.

This code runs fairly well, except there are times when TARGETA/TARGETB are no longer within the defined boundaries and send the user and target over 2,000 studs in the facing direction.

Let me know if you see any issues with my current handling that I can improve on to remove this inconsistency. Also, if you have a suggestion on a different way of handling this type of action please let me know. I have tried lerping, tweening, body movers, and other variants utilizing raycasting and each seems to have a positive and negative to it… The lerping is the closest I have gotten to perfect handling, aside from this very random occurrence…

Thanks!!
Romul

Forgive me for skimming, I don’t get paid.
TARGETB = ORIGIN*CFrame.new(0,22,-28)
Ray.new uses origin and direction, not origin and target. Unless I’m incorrect about your actual problem, you should use this instead.
Ray.new(origin.p, targetb.p-origin.p)
Also pardon capitalization, I’m on mobile. Let me know if this doesn’t fix it.

1 Like

You are absolutely right! Thanks man, as of now it is working flawlessly. I have no idea why I thought it was origin and target… Thanks!!

1 Like

Everyone thinks that at first. I suppose it makes a lot of sense to most people, and whenever someone has a ray issue it is the first thing I look for. Good luck with the scripting.