Let's talk projectiles?

I’m currently working on some projectiles at the moment; I’m dabbling around to try and find the best method of doing so.

Right now, my projectile is controlled using a for i = loop, however I’m unsure if this is the most efficient way of doing so? - I have tried using BodyMovers however the ServerLag causes issues with this, and when setting the NetworkOwner to the Player itself, there seems to be some stuttering from the Projectile as soon as it’s inserted. Hence my current method which does, however, do the trick.

I’m wondering if anybody else has any suggestions on methods I could use?

image

https://gyazo.com/922c6ea97bfba599e670eb56e069784b

So far, this is the method I use:

Code
local SpellSpeed = math.random(6, 10)
	for Movement = 1, math.huge do 
  	if not SpellPart then break end
 	wait()
  	local SpellRay = Ray.new(SpellPart.Position, SpellPart.CFrame.LookVector * SpellSpeed)
  	local Obj, Position = workspace:FindPartOnRay(SpellRay, IgnoreList) -- Is IgnoreList defined somewhere? It's not shown in your code block.
  	if not Obj then
    SpellPart.CFrame = SpellPart.CFrame + SpellPart.CFrame.LookVector * SpellSpeed
  	else
    if Obj.Parent:FindFirstChild("Protego") == nil and Obj.Parent.Parent:FindFirstChild("Protego") == nil then
      SpellPart:remove()
      local HitObj = Instance.new("Part")
		HitObj.Parent = Player.Character
		HitObj.Transparency = 1
		HitObj.Anchored, HitObj.CanCollide = true, false
		HitObj.Size, HitObj.CFrame = Vector3.new(1, 1, 1), CFrame.new(Position)
		local HitParticle = script["HitParticle"]:Clone()
		HitParticle.Parent = HitObj
		HitParticle.Enabled = true
		HitParticle.Color = SpellTrail.Color
		local HitParticle2 = script["HitParticle2"]:Clone()
		HitParticle2.Parent = HitObj
		HitParticle2.Enabled = true
		HitParticle2.Color = SpellTrail.Color
		require(Wand.Spells:FindFirstChild(SpellPart.Name)).Hit(Player, Obj)
      break
    else
	IgnoreList = nil
    SpellPart.CFrame = CFrame.new(SpellPart.CFrame.p, Player.Character["HumanoidRootPart"].CFrame.p)
      --break
    end
  end
end

You should be using a repeat loop and wait until the Ray gets the “Hit” value. Sidenote, :remove() is deprecated, use :Destroy() instead.

1 Like

Speaking of projectiles like the example you showed, is this projectile going to involve physics such as bullet drops? If not and you just simply want a part going straight through a ray while this performs instantly on client and replicates on the server at the same time, I highly recommend to utilize TweenServices

I hope I did not get your question wrong, at least I use TweenService on my guns so far, which I might change in the future

1 Like

Well nice stuff you have going on! But can’t you simply avoid using an infinite loop? From the gif you sent, you can kind of break down each step:
When the mouse clicks, you create a ray from the SpellPart to the Mouse.Hit.Position. Than you make two attachments, one at the SpellPart and one where the ray landed, and a beam between those attachments, and a particle emitter at where the ray landed.
Am I missing the usage of a loop?

1 Like

Pretty sure he is trying to simulate an effect of the spell actually “travelling” or flying to where the caster casts. That would simply make a flashing beam effect, more efficient for guns and bullets, not spells.

Yes, you’re right - I did try the other method in early versions.

The way it works is a Ray is constantly cast infront of the SpellPart that determines whether or not impact has been made.

Following on from @Headstackk - I briefly understand what you’re saying: Tweening the Part from PointA to PointB - Whilst also, at the same time, generating the Ray. This way the Tweening will act as the ‘BodyMover’?

Yeah I can notice that, but the beam almost goes instanly, and honestly it is more ideal to remove a small detail for better performance. But I’m not really forcing you to remove it, it is a cool feature, and I don’t think there would be different ways to do this,

2 Likes

Generate the ray first, then tween your part from the start of the ray to the hit point.
For my guns, I will deal damage on tween.Completed.

Because bullet moves in a pretty high speed, so I rather have tween.Completed to do the job of hit confirmation rather than casting a ray every loop to see hitting or not. This wouldn’t really affect the general gameplay a lot after I experience it.

Or you can use touch event… which I don’t recommend at all. :thinking:

I understand where you’re coming from - But I still need to factor in the possibility of Player’s avoiding Spells & protecting themselves which will be a feature? It would be much harder to do this if the action is almost instantaneous.

I see, than follow what headstack said! Use tweening which would be more efficient

Tweening can make Displacement (zigzag effect) of spells harder to create and modify if you are trying to re-make Serphos’ wands which I’m assuming from the trail.

1 Like

I’ve seen how Serphos’ wands work - The only similarity is the effect.
However, my ZigZag works by the movement of the Attachments as opposed to the actual Part.

I don’t understand this?

Also you can use Tween the size instead of Position if you’d like to create the same effect as the gif you showed up there, just an advice!

All i wanna say is Fast Cast by @EtiTheSpirit
seriously, this module is so underrated by the community i’m surprised the first answer on every projectile post is not this, its a really great resource to both use and learn.
i suggest learning from it since it’s not really too hard and confusing, a few modifications of your liking or creating a custom one based around how this module works and you got yourself one of the few possible ways of creating a near perfect projectile system

3 Likes

The position of the attachments that the trail is connected to is moved around the part to create the zigzag, the spellpart stays linear.

What do you mean about tweening size? The actual trail or part?