Make a raycast fire in the mouse's 2d position

I’ve been trying to make a raycast fire in the mouse’s 2d position, but I can’t figure out how and where to start. Here is the ray and part code. For example, I want the ray’s z axis (or what its called) to always be the player’s hat’s handle position, and I only want the ray’s y and x axis to change.

This is what it want it to be like.

	local UnitRay = Ray.new(player.Character.Hat.Handle.Position, (mouse.p -player.Character.Hat.Handle.Position).Unit * 100)
	local hit, pos = workspace:FindPartOnRayWithIgnoreList(UnitRay, ignored)
			
	local distance = (player.Character.Hat.Handle.Position - pos).Magnitude
			
	v.Size = Vector3.new(v.Size.X, v.Size.Y, distance)
	v.CFrame = CFrame.new(player.Character.Hat.Handle.Position, pos) * CFrame.new(0, 0, -distance / 2)

Help is appreciated!

3 Likes

Use Mouse.Hit.p, Mouse is a mouse object, get a mouse object from a player by:

Player:GetMouse()

Now that you’ve got the position, you can integrate it in your code. Just do the same thing but make the target position the mouse position.

1 Like

You can either use Mouse.Hit, or use your own Raycast like so:

local userInput = game:GetService("UserInputService")
local cam = workspace.CurrentCamera

-- Get mouse position:
local mousePos = userInput:GetMouseLocation()

-- Get the ray from the mouse and project it forward:
local mouseRay = cam:ViewportPointToRay(mousePos.X, mousePos.Y)
mouseRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000)

-- Cast the ray:
local target, hit = workspace:FindPartOnRay(mouseRay)

This will cast a Ray based off of the relativity between the camera viewport to mouse 2d position. Doing it custom like this allows for more options later on, such as whitelisting/blacklisting objects, and applying randomness.

I’m a little bit confused by what you mean by lock the Z axis. The origin of the Ray will always be the head’s position the way you are doing it.

If you want to set one of the axis of the LookVector to stay locked to the character’s, you can just set it to the according LookVector value of the Character’s HumanoidRootPart.

UnitDirection = (mouse.p -player.Character.Hat.Handle.Position).Unit
Direction = Vector3.new(UnitDirection.X, UnitDirection.Y, HumanoidRootPart.CFrame.LookVector.Z) -- sets Z direction to HumanoidRootPart CFrame LookVector

local UnitRay = Ray.new(player.Character.Hat.Handle.Position, Direction * 100)
3 Likes

I am already using mouse.Hit.p.

Then what’s the problem? Are you having trouble adding it to your code?

Im trying to make the ray’s z axis (or what its called) to always be the player’s hat’s handle position, and I only want the ray’s y and x axis to change. Mouse.Hit.p, changes in every axis, so i want to know where i could start to only calculate the x and y

Oh, so relative from the character you want to find where they are pointing in 3D space?

Sort of, Im trying to find where they are pointing in a 2d space

The FindPartOnRay function was deprecated, use the new Raycast function instead.

1 Like

I’m not so sure what you’re getting at, could you explain more?

I just went with the method he used in the original post for simplicities sake. While this is the correct format, we are just trying to help him understand the math here.

1 Like

I made some updates to the bottom of my original post. Is that what you are looking for?

1 Like

Yes. That is what i am looking for, i am trying it out right now.

It worked thanks a lot ! I’ve been trying to do this for a while now!

1 Like