(sorry about any typos, it’s late and I figured I’d draft this up before I signed off for the night.)Ĭlick this for a better look without all the page scrunching on the forums. It’s much better to copy a single bullet per function call and re-position it accordingly.Īnd as A final note, in my example I used wait(0), but I’d recommend switching that over to one of the RunService’s step events and just waiting for that to fire, they just sync a lot nicer and wait() can be a little inconsistent under heavy load. And copying a bullet per loop iteration puts a really big load on the script, especially when you fire lots of bullets. Getting the distance and the bullet drop amount are all just simple Newtonian physics put to use, I’m sure if you wanted to read more into them the’re are million of pages online about them.Īs per your old code I’d avoid using a for loop for this type of thing, they can aid in that stuttering if your trying to be accurate and using the values from loop. But that doesn’t really matter if you are trying to aim for a “smooth” flight. All of the distance calculations it does are all influenced by the time between iterations, so you’ll get consistent velocities 99% of the time but your ray distance wont be the same number all the time. It more or less runs until it either hits something, or it hits a distance cap. Theres a lot of redundant bits that could be cleaned up to boost performance as well as a few API calls you could localize. I didn’t really optimize a lot of this, I aimed more to keep it as simple to read as possible (no clue how well I did on that though). Thats how I’d go about setting that up anywho.Īlright, did my best to try and comment anything that’s important in how I do my bullets with rays. Once the client has hit something with that bullet, you just send the ID to the clients, and they’ll cease activity of the bullet. In order to make a bullet “hit”, you’ll need to give it a unique ID and tell each client that ID. You can then re-create the CFrame on the clients using this information. In terms of sending the data, rather than sending a CFrame, send the Starting position, and the position of the start CFrame moved 1 stud in the lookVector. Stagger the raycasts, so they’re only called every couple of updates.ĭo them per-update, but have a minimal amount of projectiles as to not slow the system down. Then, using the heartbeat/stepped method, you can iterate the projectile at any speed you’d like, using delta/tick() equations.Įssentially, how far do you want a projectile to travel per second? How long has it been since the last update? Multiply together, you get the distance the projectile should have travelled in that update.įor then using hit detection, you can do a couple of things. If you’re looking at shooting in a straight line, make sure you have your direction to begin with, keep note of the bullets “CFrame” but this can be stored as data, and a seperate CFrame can be applied onto this to make a 3d model work using it. HandleBulletHit(hitPart, hitPoint, hitNormal)īulletVelocity = bulletVelocity - (Vector3.new(0, workspace.Gravity, 0) * dt)īulletPos = bulletPos + bulletVelocity * dt Local hitPart, hitPoint, hitNormal = workspace:FindPartOnRay(bulletRay) Local bulletRay = Ray.new(bulletPos, bulletVelocity) Local dt = RunService.RenderStepped:Wait() Local bulletPos, bulletVelocity = originPoint, velocity My normal approach is something like this: local function fireBullet(originPoint, velocity) I’ve actually done this myself a few times, too. Odds are that it won’t really be that bad. I would definitely benchmark this and see how it affects your game, though. Of course, there is still a fair amount of overhead from invoking the API. So you can cast a whole bunch as long as they’re not any longer than the one single ray would’ve been. The overhead of raycasting has a lot more to do with the length of the ray(s), rather than how many rays there are. You actually don’t have to use a low resolution, either.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |