How i can detect Part(Car) is on line

What do you want to achieve?:
I want detect if part (car) is on green line
is right from line return 1
is left from line return -1
is on line return 0

What is the issue?
It works when street have no rotation.

What solutions have you tried so far?
I try use CFrame.x

code snippet:


function isOnSite(part)
    local m=0
    if part.Name=="STREET" then
        local getP=part.CFrame.X+(part.Size.X/4)
        print(scanner.CFrame.X.." == "..getP)
        if scanner.CFrame.X>getP then
            return -1
        else
            return 1
        end

    end
    return 0
end

Greetings kevini44 , thanks for help

1 Like

Please adhere to the rules of Scripting Support.

  1. What do you want to achieve? Keep it simple and clear!
  2. What is the issue? Include screenshots / videos if possible!
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Also, please format your code correctly by using syntax highlighting
IN
```lua
– code
```

OUTPUT:

-- code here

If you want to know if it is on the green line or not, could you just use Part.Touched?

Note: if the green line will not exist in the game and you want the car to center, I would think your mathematical expression would work. Maybe it’s a coding error.

the line is not real just for optic that you know what i mean

1 Like

Maybe you can use Region3 and check if car is in that region. you can also use path finding if you are working on AI I guess.

1 Like

Putting all the math stuff aside. I recommend a rabbit approach. To where the car is trying to catch up with some invisible object on a track. Then you can simply use the math on that object to follow the path instead. Might be smoother than just trying to make the car go. Interesting nevertheless. That’s all the support I can provide tho, this is pretty much beyond me.

Or disregard this post if you’re already doing that haha.

Your script wasn’t accounting for rotation.

local function Check(Part, Car)
	local DotProduct = (Part.Position - Car.Position):Dot(Car.CFrame.RightVector)
	local Side = DotProduct < 0 and -1 or DotProduct > 0 and 1 or 0
	
	return Side
end
3 Likes

Well you’re using global coordinates x axis… so…
You’ll have to implement a way to account for part rotation and define what you mean/want.
You could for example compare the car position to the position of the road part ± rightVector and see which one (positive or negative) it is closest to.

You could use the dot product on the right vector of the line and the vector between the line and the car. Or you could use the CFrame:pointToObjectSpace(Vector3) method and check the sign of the x component.

Keep in mind this is assuming the line is CFramed such that the right face of the part is facing right.

1 Like

Thank you guys, “:Dot()” was really helpfull :slight_smile: !

Please ensure that you mark a solution if your question has been resolved, so that others who have the same problem and come across your thread know where to look first.

Check out the concepts on path following. It’s pretty in-depth and may take a few days to learn properly, but it’s worth the time.

1 Like