I’m having some issues figuring out the logic behind doing a third person gun script/tool. While I am new to scripting, it’s not an issue with code it’s more so that I don’t know how to figure out the logic behind what I am looking for.
I’m aiming to create a third person shooting system similar to Fortnite(Can’t think of any other good example for now). I want the mouse to be locked at the center of the screen and your character would rotate as you moved your mouse left-right and you could aim up-down obviously. Shooting would trigger a Remote Event on the server side where it would check which way you’re aiming and then cast a ray in that direction.
Now here is where my logic fails me.
My first idea is to shoot the ray from where the mouse/crosshair is located in the direction the player is facing. However how would I determine that direction exactly is beyond me:
I can find where the Mouse.Origin is on client and forward that to the server. That gives me the position from which I am casting my ray.
Simply using the rotation of the Humanoid would only give me horizontal rotation. So that isn’t going to work.
What seems the most promising to me: I could use Mouse.UnitRay and then figure out which way the player is facing by using the direction/angle of that ray and just cast a ray going from the mouse in the same direction? Here is a bad drawing to explain what I am thinking of
I don’t know how exactly I would go about doing the math behind that though.
Perhaps I’m making it too complicated or trying to re-invent something that has a much simpler method.
Anyways this is my first Topic on the developer forum so I am really sorry if I didn’t explain anything correctly or made any obvious mistakes. Hopefully it isn’t too broad of a question either. I am not looking for someone to write the code for this or help with a whole gun/tool script. All I’m trying to figure out is the essential part of shooting and getting what the player has shot at.
So your goal is to figure out where the bullet hit or what the bullet hit or something? I don’t understand your problem correctly, It’s either my fault or something else
My goal would be to figure out how to cast a ray to where the player is aiming at in third person, which means where the bullet hit or what it hit it doesn’t really matter since I can get both from a ray.
Yes but that would not be raycasting and I would have no way to verify what the player hit on the server. As that would mean you could just send any part/position to the server and it would assume you can shoot it.
wait let me search wiki… if you wanna get a raycast you can do it by Ray.new(tool.handle.position,(mouse.hit.p - tool.handle.position).unit * range) but I’m not sure exactly this what you want
casting a gun on server side is a bad idea to create a gun since player also have to calculate player’s actual position but it also anti wall damage exploit, In client player could see that player movement while in server, they’re already idling in different position
if you are trying to cast a ray from a crosshair/ the mouse’s position, you can use the ViewportPointToRay, then you could do something like this: (example)
local UIS = game:GetService("UserInputService")
local Camera = game.workspace.CurrentCamera
local MousePos = UIS:GetMouseLocation()
local UnitRay = camera:ViewportPointToRay( MousePos.X, MousePos.Y) --- or the crosshair's position
local Range = 200
local ray = Ray.new(UnitRay .Origin, UnitRay.Direction * Range)
But how would I go about then making sure that what the player shot at is actually valid? I mean all of this information could be faked and they could simply forward any part to the server which a ray “supposedly” hit and the server would deal the damage to that?
Sounds really interesting! I’ll test this out ASAP, can’t right now, but as I posted below how would I be able to confirm someone isn’t exploiting the script to shoot anyone on the map?
Raycast in client and server are the same except in client all you gotta do is Send RemoteEvent, Hit position and Hit part or just straight up checking in client and then send the Humanoid of the target and Damage
I agree but that would literally leave it completely open to exploit. You could just kill anyone, anywhere by messing about with the code on the client.
The whole reason behind me wanting to use a ray is making sure the data is correct. I could just use mouse.Target and that would be it. If I was going for a clientside solution I wouldn’t even need a ray.
Well I got no idea about this but everything is not perfect, you lose one and get one or never got one. In term of gameplay for regular player, I would rather having raycast on ClientSide. I can think of one idea which is crazily took alot of performance so I cutted it out and try not to talk about
Perhaps I could validate the shots on the server by checking if the player you shot at is in range and other methods to make sure you aren’t exploiting it. But that is still prone to latency and would mean I should just leave it completely unprotected?
Well yeah, you can try check the range, it should atleast help abit, Unless it’s a really short range tool, otherwise you gotta expand the range abit so clientSide’s shot won’t be unvaild because of player’s movement is out of range in server side
i wouldn’t necessarily use Mouse.Target for a gun, because what if you wanted to set a range to your gun using Mouse.Target will most likely make it harder to do so.
For stuff about exploits i would also check out these threads if you haven’t already:
Actually, I got another idea and that’s raycast from client and if it hit target then when a server recieve, It will recieve a TargetHumanoid,targetposition,playerposition,Damage and then you create 2 parts between targetpos and playerpos and somehow make them got ignored in gun’s raycast, make the current raycast Ignore Every Humanoid (If you have collectionService or Humanoid Checker Table) and then raycast them in the server, If hit it that part or straight up hitting target’s part then it will register the damage, this is my typical solution I can think of instead of that idea