✓ Accepted answer
You may be familiar with the first two return values of FindPartOnRay: the part it hit and the point of intersection. There is a third value that you're ignoring, which is the surface normal. The surface normal is a one-stud long vector which is perpendicular to the face of the object.
Here's a picture I drew to illustrate what I'm talking about.
As you can see, no matter the angle of the ray, as long as they hit the same surface, their surface normals will be parallel. This is useful because this determines which direction the bullet hole will be facing.
Now assuming that you've already casted the ray, this is one way of making bullet holes:
local bulletholepart = Instance.new("Part")
bulletholepart.FormFactor = "Custom"
bulletholepart.Size = Vector3.new(1, 1, 0.2)
bulletholepart.Anchored = true
bulletholepart.CanCollide = false
bulletholepart.Transparency = 1
local bulletholedecal = Instance.new("Decal", bulletholepart)
bulletholedecal.Face = "Front"
bulletholedecal.Texture = "http://www.roblox.com/asset/?id=2078626"
function shoot()
local hit, pos, normal = game.Workspace:FindPartOnRay(ray)
local bullethole = bulletholepart:Clone()
bullethole.Parent = game.Workspace
bullethole.CFrame = CFrame.new(pos, pos + normal)
game:GetService("Debris"):AddItem(bullethole, 10)
end
Take note that you will have to add the bullet holes to the ignore list or else the bullet holes will stack with each other -- I'll leave that up to you.
2 comments
Well why not use a Touched Event on the bullet? And when it touches something the mark that appears on that surface is a part that perhaps gets welded to that surface? You should learn how to do welds with scripts and then do that or maybe you could make it so a part just gets CFramed to that position where the bullet colides and then the part is anchored... But then I was thinking if the player m
I need to get the correct surface for the decal to be on. Touched events are not good for FPS's, they do not execute as fast as Ray Casting. There will be physics collision problems, whereas Ray Casting has none.