Creating projectiles?

Now before I start, I want to emphasise that I’m opting NOT to use the FastCast API. This is solely due to me wanting to create my own method of Projectile - generation rather than relying on someone else’s work. This also helps me to advance in my coding.

NOTE:

I have no access to my laptop at the time of writing this as I’m on the night shift and looking to discuss // conceptualise.

This thread will be updated accordingly so that other Players with a similar question can perhaps find an answer within.



SUMMARY:

How can I make efficient projectiles and what methods can I trial to improve my knowledge?



Now let’s get into detail - The easiest way for me to explain my intention is simply - Magic. Wands and spells to be more specific.

I’m looking for a way the effictively move a Projectile from Point A to Point B - Efficiently detecting collisions along the way that will result in the effect taking place etc.



I have worked on something similar in the past and this for a ‘for’ loop to CFrame the spell Projectile forward whilst at the same time casting a Ray infront of it to detect collisions.

Although this was VERY efficient in detecting collisions and avoiding that pesky latency that causes projectiles to pass through objects - It wasn’t favourable for performance reasons and proved to cause significant delays.



Ideally the Projectile will be a basic Part - Size of roughly (1, 1, 1) and contain attachments for trails to attach to - Creating visuals and so forth.

Any and all help is much appreciated!

2 Likes

Incidentally, there is a new physics engine feature coming soon, which will allow you to put impulses onto an object. This could be quite useful in creating projectiles. Unfortunately there were some issues and they had to put it on hold for a short amount of time, and will hopefully be coming with the next feature update. You can read more about it here if you are interested:

But lets say that you want to do this without doing an impulse (because it isn’t out yet or for other reasons). I would agree that using a CFrame and for loop would not be sufficient for achieving what you want. Assuming that you have a wait() function in your for loop, and you update the CFrame many times per second, the server slowing down could severely cause the speed of your projectile to slow down because a wait() would take longer. I might instead suggest that you look into the BodyMovers, specifically a BodyVelocity.

With a BodyVelocity, you can apply a constant velocity to the projectile in a certain direction. This would cover the visuals and hopefully achieve what you want. However that leaves the collisions. You might consider using the projectile’s Touched event, and detect when a part owned by a character or NPC is hit. Then you could act upon this hit happening by destroying the projectile, lowering HP, etc.

Honestly, I haven’t done a projectile system like this before. But if I was to go about it then I would try this option first. Let me know what you think

3 Likes

Isn’t OOP required for this? I’m not really sure how to do this and I’d also want to how to create projectiles. Thanks for asking

1 Like

what I would do is raycast. Then tween a bullet to the raycast’s result.

2 Likes

This is probably the better method. With a BodyVelocity you still have to account for gravity and network ownership but not with this.

1 Like

I’ve tailored around with Tweening and Raycasting in the past - The issue I encountered with this is accounting for object, Players etc. That come infront of the projectiles during motion - That’s why I used the for loop as each time the Raycast would be updated infront of the Projectile, accounting for new objects.

Then perhaps using the Touched event with a raycast? I am not quite sure if the Touched event would still work while the object is being tweened, but it is worth a try. Use the initial mouse raycast for the position to tween to, but use the Touched event to detect collisions.

Using .Touched isn’t necessarily reliable due to potential delays - This could cause the projectile to pierce the target, register its hit, then delete afterwards. Causing a horrible effect.

I’m contemplating picking apart at the FC Module in order to learn how it was done then attempting my own edition tbh.

I was going to question this but then I tested it myself and it seems like you are right.

The target is unanchored and making the bullets slower makes the touched event work, so the event itself works. When it detects the hits the bullets are supposed to destroy. It doesn’t register for any of the bullets.

1 Like

I’m contemplating picking apart at the FC Module in order to learn how it was done then attempting my own edition tbh.

best way to learn, when you want to learn, go do that

Using .Touched isn’t necessarily reliable due to potential delays - This could cause the projectile to pierce the target, register its hit, then delete afterwards. Causing a horrible effect.

or just not hit and go straight through the object it’s supposed to hit, bounce off and go somewhere else, cause major lag, etc… etc…

1 Like

Thing is, that’s been a real problem for close to 3 - 4 years now. Primarily ever since the introduction of FilteringEnabled.

But after reading into your post about the delayed API, that could actually be the solution once released.

Though, truthfully, I feel as if I’m gonna have to do the one thing I wanted to avoid and go through FastCast and its 1k+ lines of code. :joy:

1 Like

General query on the topic,

Is there a way to simulate a zig-zag effect during motion as portrayed as the solution in the following post?

As I’m not too familiar with FastCast and am yet to experiment, but I know it uses Heartbeat, but does it use CFrame to move the projectile forward or is it tweened?



OOP isn’t needed for this, but it could potentially make the code more concise and make it easier to implement new features. Here’s a good resource for getting started with OOP: Metatables, Metamethods, Modulescripts, and OOP. Another good tutorial for OOP is: All About Object Oriented Programming.

P.S. - When starting with OOP, it’s best to think about it as a dictionary with special features.

2 Likes