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

How so? All I need to use is a RemoteEvent and pass the data to the server. Quite simple actually.

Have you played The Street before? You will get that feeling when you try to melee or shoot someone

if u put on Client u need more things, a remote in ReplicatedStorage so all players can see the bullet, then sending in another remote Event in ReplicatedStorage to see the Ray’s end CFrame and if the Ray hit something and the LookVector of the Ray and will be much harder becouse u will not able to see Ray’s Origin and more

To be honest, this is what most FPS programmer would do, Put Hit detection on Client so hitboxes would be better compare to server. we hate having Server hitboxes don’t we?

1 Like

Have not, but I know what you’re talking about for sure. Anyways I still need to figure out how to cast that ray in the proper direction on the client. So once I try out @Jaycbee05 's viewport idea i’ll get back to you. Thanks a lot guys

Okay, Goodbye. My job is finished I believe

for the aiming part, you will want to make the character face the mouse position. EgoMoose has a tutorial for that aspect, along with other gun-related info, here. it is oriented towards first person though

when you have the character facing the mouse position, you will raycast out from either the LookVector of the tip of the gun or the LookVector of the torso.

He said Third person and that’s first person

Nice method but only for first person, as doing it this method would leave me with an incredibly inaccurate way to cast a ray, since it wouldn’t be shooting at all towards where you are aiming with your crosshair

im confused as to how thats the case. when you change your camera direction, it changes the gun direction, which changes the ray direction. + its more accurate than firing from the camera since its exactly where the gun is pointing and where the bullet is coming from.

dada

simpler for me to explain this way

i also think the first method given is probably at lot simpler… I’ll have to figure out if it’s what I wanted tho

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.