Part goes through wall at high velocity!

Ok I am making a gun. The bullet uses Velocity to move. It is set at 1200 velocity when the gun is fired! I do not know why but it keeps going through the Test dummy and other parts. When it is to fast it just goes through the parts. Yes can collide = true for the dummy and bullet! Please help!

Roblox physics is weird, for hit detection you should just raycast from either the gun or the bullet .

im a noob scripter, how do i even use raycast
i always need to follow a tutorial

local result = workspace:Raycast(origin, vector)

where origin is the gun/bullet’s position and the vector is the ray that is being casted to detect a hit

and then? what do i do after that

You can use “RunService” & store each position as the bullet travels through those frames.

local CurrentPosition = nil -- Current Position of the bullet being stored
local Bullet = workspace.Part  -- Bullet Object
local Params = RaycastParams.new() 
Params.FilterDescendantsInstances = {Bullet} -- Also add the players character who is shooting this, because it'd hit their own character.

function HasPassedPart(Start, End) -- The "Start" argument will be the stored "CurrentPosition" variable. & End will be the "in game" position of the bullet.
	local vector = End - Start
	local result = workspace:Raycast(Start, vector, Params)
	return result
end

while Bullet:IsDescendantOf(workspace) do
	CurrentPosition = Bullet.Position
	game["Run Service"].Heartbeat:Wait() -- (https://developer.roblox.com/en-us/api-reference/event/RunService/Heartbeat)
	if (not Bullet or not Bullet:IsDescendantOf(workspace)) then return end
	local result = HasPassedPart(CurrentPosition, Bullet.Position)
	if (result) then
		--The bullet went through a part, now do whatever you want with the RaycastResult
		local HitPart,Position,Normal = result.Instance, result.Position, result.Normal
		print("Hit: "..HitPart:GetFullName())
        return
	end
end

Sorry if this is messy! Just a rough script
This will be very performance costly if lots of bullets are being fired.

Here is a rushed diagram I made of what the script tries to achieve

3 Likes

Roblox physics isn’t like real world physics.
If you shoot a bullet in real life, it will travel and hit the first object it comes in contact with.
Because this is working on a computer that is trying to fit a lot of changes in physics and scripting into each frame if your bullets are too fast then they will end up skipping ‘space’ in between frames when the CPU determines the bullet should be at Position X,Y,Z in one frame and in the next frame it is at a different Position X,Y,Z. If the wall is between those two Positions then the hit won’t register.

You should read the “Manage Physical States Carefully” section of Task Scheduler | Roblox Creator Documentation.

1 Like