Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Can you explain how I can do this? (find direction a part is traveling)

I'm not posting any code this time because I'm just asking for an example.

I want to use two non-collidable parts (A and B) placed next to each other to tell the direction a part is traveling. I don't need the exact direction (not a ray or anything), but whether the part hit A then B or B then A. If you're curious, this is to tell if a train is traveling inbound or outbound on a track (only two options for direction).

In the workspace I have a part named A and a part named B, both have CanCollide set to false so that a part would pass through them uninterrupted. Can you use that to tell the direction?

Also, I need this to work even if multiple parts pass through the two objects at the same time.

Sorry for all the reading, BTW

3 comments

0

So you want to see wheter a moving part would hit A or B first?

0

Is there a limit of one train per track?

0

Yes, and I want this to work for every train that passes the two parts.

1 answer

Hey Diezel_1K, I'm not quite sure if I've understanded your question completely,

But from what i'm thinking you want to get the direction of a moving part, and then see if it would hit either part A or B

My way of doing it

If you have 2 positions we can use the .unit to get a unit direction (direction with as lenght ~1)

So.. in lua that would make

local part = script.Parent -- the part
local pos0 = part.Position; -- just for setting it back, not neccessary
part.Anchored = false; -- not moving
wait() 
local pos1 = part.Position; -- getting position1 and waiting .2 seconds
wait(0.2);
local pos2 = part.Position; -- getting position2
part.Anchored = true;
part.Position = pos0;

local unitVector = (pos2-pos1).unit; --getting the direction
print(unitVector); --printing it

For the second part, determining wheter the direction would go to part A or B you could use raycasting with a whitelist (FindPartOnRayWithWhitelist) and only allowing part A and B and seeing if the "hit" equals to A or B, not going to include that code here but if you have trouble with it I might be able to help you

This discussion is preserved as it was. New answers, comments, and votes are closed.