Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Problems with :FindPartOnRay. How could I fix it?

Asked by 9 years ago

Hi, I don't have a possible idea how to fix this...

    local rayFront = Ray.new(basePart.Position, (basePart.CFrame * CFrame.new(0, -500, 0)).p)
    local rayBack = Ray.new(mainPart.Position, (mainPart.CFrame * CFrame.new(0, -500, 0)).p)
    local part1, positionFront = workspace:FindPartOnRayWithIgnoreList(rayFront, parts)
    local part2, positionBack = workspace:FindPartOnRayWithIgnoreList(rayBack, parts)

    visualize(mainPart.Position, positionBack, "Cyan") -- based on wiki's ray's example
    visualize(rayBack.Origin, rayBack.Direction, "Deep orange")
    visualize(basePart.Position, positionFront, "Cyan") 
    visualize(rayFront.Origin, rayFront.Direction, "Deep orange")

Results: link Place: Development Slot 2

The given position from the method isn't a point of the ray I created, resulting in unwanted positions. How could I fix this?

1 answer

Log in to vote
1
Answered by
noliCAIKS 210 Moderation Voter
9 years ago

You didn't state what exactly the problem was, but looking at the following line:

visualize(rayBack.Origin, rayBack.Direction, "Deep orange")

It's clear that the second argument should be the direction (and length, since it's not a unit vector) of the visualized ray, not the target position. However:

visualize(mainPart.Position, positionBack, "Cyan")

The above uses the target position as the second argument. To fix this, you should convert it like this:

visualize(mainPart.Position, positionBack - mainPart.Position, "Cyan")
visualize(basePart.Position, positionFront - basePart.Position, "Cyan") 

Edit: Now that I see your visualize function, I realise it's the other way around, sort of. Just think; if the orange visualization is correct, what does that say about rayBack.Direction and rayFront.Direction? It means they are equal to the end position of the ray instead of the direction! That's why the ray isn't cast correctly, resulting in the incorrect position and direction of the blue visualization.

To fix this, you should change the second parameter to Ray.new to give the direction rather than the end position, like this:

local rayFront = Ray.new(basePart.Position, (basePart.CFrame * CFrame.new(0, -500, 0)).p - basePart.Position)
local rayBack = Ray.new(mainPart.Position, (mainPart.CFrame * CFrame.new(0, -500, 0)).p - mainPart.Position)

Which is also equivalent to this (the cframe minus the position to get the orientation, and that multiplied by the offset):

local rayFront = Ray.new(basePart.Position, (basePart.CFrame - basePart.Position) * Vector3.new(0, -500, 0))
local rayBack = Ray.new(mainPart.Position, (mainPart.CFrame - mainPart.Position) * Vector3.new(0, -500, 0))

Then, if you still want the orange rays to work as well, you should change the visualization part to:

visualize(rayBack.Origin, rayBack.Origin + rayBack.Direction, "Deep orange")
visualize(rayFront.Origin, rayFront.Origin + rayFront.Direction, "Deep orange")

And you can leave the cyan visualizations as they were originally.

0
I made the function, and the second parameter was the target position, sorry for not being clear. I updated the question. In my logic, if you highlited from the origin to the target point, and the origin to the direction, it should coincide FieryEvent 185 — 9y
0
@ oioiamigos Have you tried using my solution, though? It looks like the blue ones are the ones that are messed up in the picture, meaning the orange ones are correct, and the orange ones use the direction as the second parameter, not the target position. noliCAIKS 210 — 9y
0
@noliCAIKS I've tried, there's no point of changing the parameters of visualize function, as long you don't persist. If the orange is the ray, shouldn' be positionFront/positionBack on it's z, x axis? (As long the hovercraft is parallel to the ground) http://pastebin.com/fnYLmwFZ FieryEvent 185 — 9y
Ad

Answer this question