As you can see from the image, I want the mouse hit to not go beyond the origin’s position. If anyone knows about this, I’d like to know.
Are you trying to translate the mouse hit position to a similar relative position on the yellow box?
You could get the Mouse’s relative position like this:
local posX = mouse.X
local posY = mouse.Y
And then apply it to the position relative to a part:
point.CFrame = CFrame.new(posY, posX, origin.Z)
This works if its scaled 1:1 with the screen dimensions (ViewSizeX, ViewSizeY), but if not, you can just apply a scaling factor to the respective positions and it will work as intended.
Your question is almost exactly like this and you might be able to use the answer I gave here: How would I be able to get a point where a line intersect
As for what this is called, this would usually be called projecting a vector on to a plane.
That’s because I’m still the one who asked the question.
Oops. Is your plane always aligned with one of the world space planes? I.e. is it always the world XY, or XZ plane? This simplifies the solution slightly if so.
The plane depends on the surface GUI’s normal ID; for front and back, it is XY, while left and right are ZY, and top and bottom are ZX.
I will answer your question more directly but I did do something similar to this a while ago to create a holographic sight. In fact, it does what you’re asking but in reverse to give the illusion of a holographic object at a constant position relative to the plane. Check it out and see if the math makes sense: Holographic Sight - Roblox
The part that’s relevant to you is here:
--Draw a line from the virtual position to the camera
--Local space
local dir = virtualReticlePos - glass.CFrame:PointToObjectSpace(workspace.CurrentCamera.CFrame.Position)
--Find where this line intersects the glass
local intersection = (dir / dir.Z) * focalPlaneDist
Note that on the first line we make sure all our vectors are in local space relative to the part. This could be any CFrame whose Z direction is the normal of the plane we care about, it just happens to be a part’s front surface in this case. dir is a vector (in local space) that goes from the camera to a point on the other side of the part, crossing through the plane. By dividing it by its own Z component, we get a shortened vector with the same direction, whose Z component is 1. Since it’s 1, if we multiply it by the distance to the plane, the Z component must now be exactly that distance, and thus our vector must point directly onto the plane, while having the same direction.
Here’s is what I came up with:
local diff = a - b
local position = origin + diff / diff.Z
All that is left to do is get the difference of origin and the new position then get the magnitude and subtract that to the target’s position.
You’re missing the multiplication step:
local position = origin + (diff / diff.Z) * distanceToPlane
where distanceToPlane must be the projected distance from origin to the plane. That is the shortest distance from a point to a plane, which always forms a right angle on the plane. You can get this by taking the dot between the plane normal and the difference vector between camera pos and any point on the plane:
I don’t think this is the solution. It does not stay on a constant Z axis relative to the origin, but it is close, so I’ll keep testing and see what I get.