Trying to move player as far as possible before they hit a wall.
Everything mostly works, but when the player is turned to face the opposite direction, the raycast is going off to the right, rather than straight ahead.
The console doesn’t produce any errors.
if rayresult then
local hit = rayresult.Instance
if Character.HumanoidRootPart.Position.X > rayresult.Position.X and not debounce and not turning then
debounce = true
if rayresult.Instance.Name == "Wall" then
print("Moving player")
local tween = TweenService:Create(Character.HumanoidRootPart, tweeninfo, {CFrame = CFrame.new(rayresult.Position.X + 2, rayresult.Position.Y, rayresult.Position.Z) * (Character.HumanoidRootPart.CFrame - Character.HumanoidRootPart.Position)})
tween:Play()
wait(3)
debounce = false
else
A snippet of code. Any idea why it’s offsetting when turning around to face the opposite direction?
Based on the snippet, it’s probably related to the arbitrary +2 in the CFrame of the tween properties.
If you posted more of the code (specifically the raycast) it’d be easier to verify the problem.
local rayparams = RaycastParams.new()
rayparams.FilterDescendantsInstances = Character:GetChildren()
rayparams.FilterType = Enum.RaycastFilterType.Blacklist
local rayresult = workspace:Raycast(Character.HumanoidRootPart.Position, Character.HumanoidRootPart.CFrame.LookVector * 20)
Sorry, post was misformatted, and cut out the raycast. This is the Raycast with RaycastParams.
EDIT: The + 2 is to achieve a grid-like system of movement, so the player goes in the center of a square.
The way you should approach this is visualization. Make the HumanoidRootPart visible, place color coded parts on the postion/cframe nodes you create, and create arrows/lines for raycasts. That way you can isolate and identify the problem.
I don’t really know how the +2 on the x-axis accomplishes a grid-like system. It will always offset your target by 2 studs to the East.
I made a part follow the ray’s path, and it’s going the correct direction. And your last statement is what made me make the change that fixed it. I subtracted from the Z rayresult rather than X, and it works. Going to try fixing similar bugs. Thanks so much for your help!