How do I make Body Velocity projectiles go into a Zig-Zag/Lightning pattern?

  1. I want to be able to use BodyVelocity to make my projectile go in a lightning/zig zag pattern.

  2. The issue is that I don’t know how to do this because of my lack of scripting knowledge.

  3. I’ve tried looking for solutions on the developer forum but I just couldn’t find anything that I could understand or that could help me.

What I want to do: Be able to use Body Velocity to make my projectile go in a Zig-Zag pattern and towards my mouse at the same time.(If you have another way of making a Zig-Zag projectile then you can tell me.)

local BodyVelocity = Instance.new("BodyVelocity")
	BodyVelocity.MaxForce = Vector3.new(50000000,50000000,50000000)
	BodyVelocity.Velocity = CFrame.new(Main.Position,Mouse.Position).LookVector * 300

	BodyVelocity.Parent = Main -- My Body Velocity Code

Here is a video of what I want to do: As you can notice the player shoots red bolts out of their hand that goes into a Zig-Zag formation: Marvel Infinity Lag test [New Scripter!] Wanda vs Wanda - YouTube

2 Likes

The thing in the video might not be done with BodyVelocities, but by directly manipulating the CFrame every RenderStepped.

If you really want to use the physics system, try having one physics constraint that moves the projectile directly towards the target, and another one that applies a random perpendicular force to the target that changes a couple times a second.

Like Align Position Constraint?

Sure, that could work I think. Although I’d use a VectorForce for the “random offset” force and not an AlignPosition. So 1 AlignPosition and 1 VectorForce in combination.

Alr thank you! I will try that! If it works I will put your post as the solution! Thank for the help!

1 Like

Alr, so I did 1 AlignPosition and I am now on VectorForce but I basically don’t know what to do. Is there a type of vector force I have to use?

Nvm I got it. lemme try to set up Vector Force

I tried a few things and here’s what I got:

StarterPlayerScripts.ProjectileService
local RunS = game:GetService("RunService")

function randDir2()
	local dir
	repeat
		dir = Vector2.new(math.random() - 0.5, math.random() - 0.5)
	until dir.Magnitude <= 0.5
	return dir.Unit
end

function showWeirdoProjectile(startAttachment, targetAttachment)
	local p = script.WeirdoProjectile:Clone()

	p:SetPrimaryPartCFrame(CFrame.new(startAttachment.WorldPosition, targetAttachment.WorldPosition))
	p.LinearEnd.CFrame = CFrame.new(targetAttachment.WorldPosition, startAttachment.WorldPosition)
	p.LinearEnd.BallSocketConstraint.Attachment1 = targetAttachment

	p.Primary.AlignPosition.Attachment1 = targetAttachment
	p.Parent = game.Workspace

	while p.Parent do
		local randDir = randDir2() * 2900
		p.Primary.VectorForce.Force = Vector3.new(randDir.X, randDir.Y, 0)

		if (p.PrimaryPart.Position - targetAttachment.WorldPosition).Magnitude < 4 then
			p:Destroy()
			return
		end

		wait(0.1)
	end
end

function showZigZagProjectile(startAttachment, targetAttachment)
	local p = script.ZigZagProjectile:Clone()

	p:SetPrimaryPartCFrame(CFrame.new(startAttachment.WorldPosition, targetAttachment.WorldPosition))
	p.Primary.AlignPosition.Attachment1 = targetAttachment
	p.Parent = game.Workspace

	while p.Parent do
		local randDir = randDir2() * 2
		p.Primary.Attachment.Position = Vector3.new(randDir.X, randDir.Y, 0)

		if (p.PrimaryPart.Position - targetAttachment.WorldPosition).Magnitude < 4 then
			p:Destroy()
			return
		end

		wait(0.2)
	end
end

while true do
	wait(0.5)
	showZigZagProjectile(
		game.Workspace.A.Attachment,
		game.Workspace.B.Attachment
	)
end

The thing I described earlier is what I ended up calling the “weirdo” projectile because it doesn’t really… zig or zag:

I ended up having to also use a PrismaticConstraint and a BallSocketConstraint at the target to keep the projectile pointing towards the target.

Here’s the more zig-zag-like one I made:

It just randomly sets the Position of the Attachment inside the projectile to cause the zigging and the zagging, and lets the AlignPosition home in over time. ApplyAtCenterOfMass is turned off.

Here’s a place file with my tests:

ProjectileTests.rbxl (40.4 KB)

Play around with the different numbers, they really impact the look and feel of it a lot

2 Likes

Thank you so much, I will try to script something like this myself!

I have a question, how do I activate it in your place, it seems to not be starting up.

You have to test/play the game, not just run it

alright thanks for the feedback

1 Like

Also if you want to, may you tell me how to make it more sharp?

1 Like

I really don’t know, but try adjusting the MaxForce on the AlignPosition, and increasing how much the projectile Attachment gets offset (so try local randDir = randDir2() * 5)

Alr thank you, im really grateful for your help

1 Like