Richeting Bullet not working

I am trying to make a ricocheting bullet and at first, it appeared to be working, but after some testing, I found some strange inconsistencies why is this happening? (Video and code below) Its4Realzies's Place Number_ 37 - Roblox Studio 2021-05-27 17-37-37


		--Creates bullet
		local bullet = Instance.new('Part')
		bullet.Name = 'Bullet'
		bullet.CanCollide = false
		bullet.Color = sP.Color
		bullet.Material = 'Neon'
		bullet.Shape = 'Ball'
		bullet.Size = Vector3.new(bulletsize, bulletsize, bulletsize)
		bullet.CFrame = sP.CFrame
		
		
		--Creates bodyVelocity
		local bodyVelocity = Instance.new('BodyVelocity', bullet)
		bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		bodyVelocity.Velocity = bullet.CFrame.lookVector * sP.Speed.Value
		
		bullet.Parent = sP
		
		game.Debris:AddItem(bullet, sP.BulletDeletionDelay.Value)
		bullet.Touched:Connect(function(part)
			if part.CanCollide and part ~= sP and part.Parent ~= sP then
				local h = part.Parent:FindFirstChild'Humanoid'
				if h and part:IsDescendantOf(plrCharacter) then
					bullet:Destroy()
					game.ReplicatedStorage.DamageEvent:FireServer(sP.Damage.Value)
				else
					local startCFrame  = bullet.CFrame 
					local normal = startCFrame.lookVector
					local position = startCFrame.p
					local ray = Ray.new(position, normal * 500)
					local hit, position, surfaceNormal = game.Workspace:FindPartOnRay(ray) --Cast ray

					if (hit) then
						--Get the reflected normal
						local reflectedNormal = (normal - (2 * normal:Dot(surfaceNormal) * surfaceNormal))
						--Set new normal
						normal = reflectedNormal
					end
					
					bullet.BodyVelocity.Velocity = normal * sP.Speed.Value
				end				
				
			end
		end)

1 Like

Hmm, not to sure, but on the part where it checks for character it seems that it will only hit if the Character is your own? Not to sure what the “plrCharacter”. Otherwise for your problem try putting in some print commands? It can help a lot. Also maybe set up a ignore descendants list for the ray?

1 Like

What kind of print commands? And ignore what?

You might want to ignore the bullet? Otherwise for the print command I would place one in each if command, not to sure what is happening though, you could also try to print the reflect values, although those are harder to read.

Also maybe try to print the name of the part that it is reflecting off of.

1 Like

Its strange because the first bounce always seem to work fine but there is no difference in code from first and second bounce

Mhmm, I haven’t seen this before, but I would recommend printing the object which the ray bounces off of’s name still, that would probably solve it.

1 Like

they all appear to be the same

Hmm, there is 1 part 3 in there in the middle of the output, strange.

Sorry, a bit hard for me to see considering that I don’t know the order, but I’ll try

Is the first slanted part called Part1? and the second Part2?

1 Like

Yes It goes in the order the bullet bounces so one is one and the final bounce is three

Hmm, looks as if the bounce velocity is the same thing each time strangely. I’ll look again