Projectiles become really inconsistent when pointed at an enemy

Hello Developers!

I am having an issue within my Bullet-Hell game. I’ve successfully created a simple projectile script but there is an inconsistent problem within my script and it’s seen when pointing at a moving enemy. Even if the player literally pointing against the enemy, you can clearly see that:

  1. The projectiles are moving left or right rather then just straight.
  2. The projectiles don’t start at the players PrimaryPart.

This is a Server-Script that handles the projectile

game:GetService("ReplicatedStorage").Events.FireBullet.OnServerEvent:Connect(function(Plr)
	local character = Plr.Character or Plr.CharacterAdded:Wait()
	
	local LinearVelocity = Instance.new("LinearVelocity")
	local Attachment = Instance.new("Attachment")
	game:GetService("Debris"):AddItem(LinearVelocity,0.5)
	game:GetService("Debris"):AddItem(Attachment,0.5)
	LinearVelocity.Attachment0 = Attachment
	LinearVelocity.MaxForce = math.huge
	LinearVelocity.VectorVelocity = (character.PrimaryPart.CFrame.LookVector*300)
	
	local Bullet = script:WaitForChild("Part"):Clone()
	game:GetService("Debris"):AddItem(Bullet,0.5)
	Bullet.Size = Vector3.new(0.5, 0.5, 0.5)
	Bullet.BrickColor = BrickColor.new("Bright red")
	Bullet.Reflectance = 0.2
	Bullet.CanCollide = false
	Bullet.Parent = game.Workspace
	Bullet.CFrame = character.PrimaryPart.CFrame
	Bullet:SetNetworkOwner(Plr)
	
	local FireSound = Instance.new("Sound")
	FireSound.SoundId = "rbxasset://sounds//paintball.wav"
	FireSound.Volume = 1.5
	FireSound.Parent = Bullet
	FireSound:Play()
	
	local CanDamage = true
	Bullet.Touched:Connect(function(OnHit)
		local Humanoid = OnHit.Parent:FindFirstChildWhichIsA("Humanoid")
		if Humanoid and Humanoid.Health >= 1 and CanDamage == true and OnHit.Parent ~= character then
			CanDamage = false
			Humanoid:TakeDamage(15)
			for i=0, 3 do --Splatter
				local Part = Instance.new("Part")
				game:GetService("Debris"):AddItem(Part,1.2)
				Part.Parent = game.Workspace
				Part.Size = Vector3.new(1,.4,1)
				Part.BrickColor = BrickColor.new("Bright red")
				local Velocity = Vector3.new(math.random(-1,1), math.random(0,1), math.random(-1,1))
				Part.AssemblyLinearVelocity = 15 * Velocity
				Part.CFrame = CFrame.new(OnHit.Position + Velocity, Velocity)
				task.wait()
			end
			game:GetService("Debris"):AddItem(Bullet,0)
		end
	end)
	
	
	LinearVelocity.Parent = Bullet
	Attachment.Parent = Bullet
end)
1 Like

It’s awesome that you’re working on a Bullet-Hell game. From your description, it seems like you’re facing a couple of issues with the projectiles not moving straight and not starting from the player’s PrimaryPart. Let’s dive in and see what’s going on.

  1. Projectiles Not Moving Straight: The issue here might be with how you’re calculating the direction for the projectiles. You want them to move in the direction that the player is aiming. In your code, you’re using character.PrimaryPart.CFrame.LookVector*300 to calculate the direction. This might be causing the bullets to move slightly left or right.To fix this, you should normalize the LookVector to ensure the direction is consistent and doesn’t deviate left or right. Here’s how you can adjust that line of code:

luaCopy code

LinearVelocity.VectorVelocity = (character.PrimaryPart.CFrame.LookVector * Vector3.new(1, 0, 1)).unit * 300

By multiplying the LookVector with Vector3.new(1, 0, 1) and then normalizing it (unit), you ensure that the bullets only move forward in the direction the player is facing.
2. Projectiles Not Starting at PrimaryPart: It looks like you’re setting the bullet’s CFrame to the player’s PrimaryPart’s CFrame, which should position the bullets at the player’s location. However, it’s possible that the projectile’s collision or the character’s PrimaryPart might have an offset that’s causing the issue.To ensure that the bullets start from the player’s PrimaryPart, you can adjust the bullet’s CFrame like this:

luaCopy code

Bullet.CFrame = character.PrimaryPart.CFrame * CFrame.new(0, 0, -2) -- Adjust the offset (in Z direction) if needed

This will position the bullet slightly behind the player’s PrimaryPart along the direction they’re facing.

Give these adjustments a shot (pun intended), and hopefully, your projectiles will start behaving as you intend. Keep up the good work on your Bullet-Hell game! If you have any more questions or issues, feel free to ask. Happy coding! :rocket:

1 Like

Alright, I have decided to rewrite the issue, it does work, but the problem still happens when the target is moving.

EDIT: Nvm, it was the pathfinding script that was making your advice not effective, thanks though.

I’m not mad: if it works it works, but why just copy ChatGPT’s answer in its entirety as a response? You didn’t even remove the “Copy code” button that you accidentally copied along with the response.

I love taking advantage of AI when programming, but I always at least review what it’s said before blindly pasting it somewhere. Just copying and pasting isn’t going to get you anywhere, especially while trying to help other people since it will often either write a load of nonsense or extremely overcomplicate things.

Anyone can use ChatGPT, so when they ask a question on the devforum, assume they’re looking for actual answers.

Just a suggestion.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.