Help with logic behind a third person gun/tool script using raycasting

I have a feeling this is wrong because I didn’t understand your problem that much but,

Using :ScreenPointToRay(), which basically creates a ray in the 3D world, using 2D coordinates.
screenpointto
Hoping that you understand that, what we can do is create a ray starting from the Mouse’s position, or even the Mouse’s crosshair.

local mouse = game.Players.LocalPlayer:GetMouse() --our mouse
local camera = workspace.CurrentCamera --we're gonna need the camera, because the :ScreenPointToRay() method belongs to the camera object

local ray = camera:ScreenPointToRay(mouse.X, mouse.Y)

And that ray there should be the ray you wanna use!

Now a very important note here is, :ScreenPointToRay() returns a unitray, meaning a ray that has the length of 1, and obviously it’s a gun so we need it to go further out, thus we need to create another ray, that has the same .Origin and .Direction as the ray we got from :ScreenPointToRay(), but longer.

local unitray = camera:ScreenPointToRay(mouse.X, mouse.Y)
local ray = Ray.new(unitray.Origin, unitray.Direction * 100) --I made it 100 time longer, you can make it as long as you want

And finally, ray should be the ray you’re looking for!

6 Likes

Ok, re-read your situation, and maybe this will help you

1 Like

Thanks for these replies and photos! I just implemented the method you sent me and it works great! Thank you.

But what does the second photo you provided explain? Are you visually explaining the thing I was going over early with @KollowMC about casting another ray on the serverside to confirm if that shot was possible?

1 Like

Yeah, that’s the method that you suggested at the start.
image

1 Like

Okay! Thank you. One last thing I’m trying to work out now is how I would be able to cast that check ray on the server side. To put it shortly, how would I cast a ray by having two positions instead of a position and a direction. And then shooting that ray from one position towards the other and checking what it hits obviously.

1 Like

The origin would be the first position, and the direction would be the 2nd position’s .Unit, in other words the direction of that 2nd position. Any vector has a .Unit priortiy, it’s the direction of that vector, which is expressed as a unit vector, which is basically a vector with the length one.

@starmaq

The left one is the server-side ray and the right one is what it should look like in theory?
I’m casting a ray from the players head to the position of the dummy which is located where the first shot hit, so:

local checkRay = Ray.new(playerHead.Position, rayDummyClone.Position.Unit * checkRayRangeMax)
local checkPart, checkPosition = workspace:FindPartOnRay(checkRay, player.Character, false, true)

But instead the direction is completely messed up and only goes up at that angle no matter what?

1 Like

it should be Ray.new(playerHead.Position,(rayDummyClone.Position - playerHead.Position).unit * range)

1 Like

Okay! That worked perfectly thank you! Right now I’m destroying the part as soon as I’m done with checking it. It even seems to be so quick that the client doesn’t even get to register it being added into the workspace. Which means I don’t have to worry about ignoring it when raycasting on the client, since doing that would mean I would need to create an array and a loop to get all of the children with the same name.

TL;DR It works.