Ray going halfway to its destination, help!

If you read this and you know about Raycasting, please replicate this and tell me if it happens to you. The ray is going halfway to its destination. Look at the workspace and do the same thing as me, then click Run, and then insert a part and put it in the middle of them. Then start moving that Part a bit to its destination and you will see it will start printing nil. Why is this happening?

Look at it:

The 2nd argument in a raycast, Direction, is a directional vector, not a position. According to the wiki:

The directional vector of the ray. Note that the length of this vector matters, as parts/terrain further away than its length will not be tested.

An easy way to get this vector would be to subtract the position of the origin part from the position of the destination part.


1 Like

Thank you for this great explanation but, what do you mean by Directional Vector though?

EDIT: I saw on the wiki and it says is a Vector3.

A Vector3 is just a data type for 3 numbers, it can represent many things. Usually it’s a 3D position in space, but it can also represent a direction. For example a vector of (1,0,0) would be pointing in the positive direction of the x axis, with a magnitude/distance of 1. The wiki could probably explain it better https://developer.roblox.com/en-us/api-reference/datatype/Vector3

Yeah I know about the Vector3 DataType, I always use rays and they weren’t failing like this before. Idk what I am doing wrong that is going halfway.

EDIT: I send as a second parameter a Vector3 which is the direction of the ray.
EDIT 2: Doesn’t the ray just draw itself after giving it two points?

You’re using the position of the destination part when you should be using a vector that points toward that part

To add to @Haggie125’s answer, you can get a directional vector from two position points by doing destination - origin. This will give you a direction vector who’s distance is equal to the distance from destination and origin. If you want to always have a ray cast, say 75 studs, you can normalize this vector and multiply it by a constant.

direction.Unit will ‘normalize’ the direction vector to be a length of 1. So then you can set the ray’s length with direction = direction.Unit * 75

1 Like

OK, now I understand lol. I forgot about it, thank you!

The second parameter if the Raycast function is the Direction, not the Destination. So, instead of doing that you subtract the destination point by the origin point and you’ve got your direction:

CheckForParts(Model.Part.Position, Model12.Part.Position - Model.Part.Position, Model12)

As a side note, you forgot to set the type of filter descendants, either whitelist or blacklist:

Settings.FilterType = Enum.RaycastFilterType.Whitelist
1 Like