Hello! I´ve been working on a weapon system for a game and I’ve ran into a bit of an issue.
Some weapons in the game are hitscan while others are projectile.
The weapons origins aren’t exactly on the camera and to compenstate for that I cast a ray from the camera to see what position the weapon should fire towards instead of just from the camera’s look direction. (I don’t use Mouse.Hit because the projectiles should ignore friendlies and players should be able to aim through teammates, but it’s effectively the same)
This works great for hitscan but projectile weapons seem to be extremely difficult to hit and leading the shots often sends it slightly off from where you’d expect it to hit.
It’s possible my calculations are wrong somewhere so I’m asking if there’s something more I should consider when calculating the direction of projectile bullets or if I should double check my raycasts.
Hello! Sounds like a neat system and I’d be happy to help, but in order to do that, I’m going to need more information about the system you’ve made:
What method are you using to detect projectile collisions?
How are you moving the projectiles? Velocity? Constraints? CFrame?
Are you handling the projectiles on the client, server, or a mix of both?
If both, what specifically does each side handle?
The .Touched method is notorious for being inconsistent in scenarios involving high speeds or small parts, of which this includes both, so if you’re using that this could be a starting point to improve upon.
The projectile requires laser accuracy to hit a target since its using raycast for detection.
You’re raycasting every Hearbeat which can be costly on computational resources and almost certainly will increase network latency and make the bullets harder to aim.
Because your problem has a lot of factors at play, only some of which can be accounted for, there’s not a lot of great solutions. A few starting ideas:
Difficulty with hitting and leading projectiles may be offset by changing Raycast for Spherecast as this has a volume of detection.
Reducing network latency may help with leading shots, although it sounds like your problem is more being worsened by lag rather than actually being caused by it. But –
You could consider adjusting the RaycastMagnitude/FrameRate ratio such that you increase the length of the Raycast segments so that you only have to raycast every “x” number of heartbeats. This would preserve the projectile velocity while reducing computational load at the expense of a reduced refresh rate / less smooth appearance. The Microprofiler can likely help find the spot where performance is optimized against visual appearance.
FastCast is modular though, and i’m not sure how much effort this would require.
Hi, I appreciate your suggestions, I’m almost certain network latency isn’t the issue or performance both have been reasonably good with the systems I’m using.
I actually was able to figure out the issue myself, the problem was in the calculations I was doing.
When looking into the skybox, (like when I was leading a shot from a moving target) I had the projectiles fire towards a point 100 studs away from the Camera’s look direction, changing that number to be much higher made the projectiles more accurate. Otherwise they do land where you would expect when drawing rays towards objects that aren’t the skybox.