When creating a ray, it is important to note what the two parameters are.
As shown here, a ray requires a Vector3 origin
and a Vector3 direction
.
You have correctly input the Vector3 origin
parameter, assuming you want it to be 10 studs above the Vector3 dub.Default.Position
.
You have incorrectly input the Vector3 direction
parameter, as it should be a unit vector
multiplied by a set range, which would be a numerical value. All that this means is that rather than using a target location, you should be using a direction. There are a couple ways you can go about doing this:
1) Using set directions and multiplying them by a set range
This method is quite simple, the basic directions are as follows:
- Up: Vector3.new(0,1,0)
- Down: Vector3.new(0,-1,0)
While there are more directions than this, it is harder to visualize, and they would be on the world axis, causing issues with anything you might need them for, except advanced purposes.
2) Converting the target to a Vector3 direction
by using an inputted origin, target, and range.
This method is also rather simple, first you take the target Vector3 position
, subtract the origin Vector3 position
, take the unit vector of the outcome, and then multiply it by a set range, outputting a Vector3 direction
parameter.
Here's a code example of what I just said:
1 | local function toDirection(origin,target,range) |
7 | return (target-origin).unit*range |
This function will convert an inputted origin, target, and range into a Vector3 direction
parameter, so that you can create your ray.
Back to the original question, you want to find out where the floor actually is, so you will likely want to use a simple unit vector going in the "down" (Vector3.new(0,-1,0)
) direction, and a range of 100.
Here's a code example of what I just said:
10 | local origin = dub.Default.Position |
11 | local down = Vector 3. new( 0 ,- 1 , 0 ) |
14 | local direction = down*range |
17 | local ray = Ray.new(dub.Default.Position,direction) |
18 | local hit,pos = workspace:FindPartOnRayWithIgnoreList(ray,ignorelist) |
Now this is not all that you wanted to do, you also wanted to set a model's CFrame to have it's old Position, but to contain a new Y value. This would be done using what is listed above and then adding on to it in the way you did.
While this may not be effective at keeping the old rotation, it will certainly change it's position. If you are interested in keeping it's rotation, please let me know and I would be happy to help.
Here is a finished code example of what you are looking for:
11 | local origin = dub.Default.Position |
12 | local down = Vector 3. new( 0 ,- 1 , 0 ) |
15 | local direction = down*range |
18 | local ray = Ray.new(dub.Default.Position,direction) |
19 | local hit,pos = workspace:FindPartOnRayWithIgnoreList(ray,ignorelist) |
21 | local newpos = Vector 3. new(dub.Default.CFrame.X,pos.Y,dub.Default.CFrame.Z) |
23 | dub:SetPrimaryPartCFrame(CFrame.new(newpos)) |
If you have any issues, or something isn't quite right, please let me know. I do however wish to know more about the issue you were having, since this may perhaps not be all that you need to do to solve the total issue you were having.
If you found this helpful, please give me an upvote and accept my solution as it makes the time spent answering this question in-depth worth it, and inspires me to provide more in-depth solutions to others!