FastCast Module - Bullet ricochet firing straight back at player

  1. What do you want to achieve?
    I want the player’s bullets to not fire straight back at them when they fire their gun directly at a wall/at a small angle.

  2. What is the issue?
    The player’s bullets fire unrealistically ricochet straight back at them.

  3. What solutions have you tried so far?
    Searched the forums but found none dedicated towards this type of problem

For context I am using the FastCast module

Relevant code:

local function Reflect(surfaceNormal, bulletNormal)
	return bulletNormal - (2 * bulletNormal:Dot(surfaceNormal) * surfaceNormal)
end


function OnRayPierced(cast, raycastResult, segmentVelocity, cosmeticBulletObject)
	-- You can do some really unique stuff with pierce behavior - In reality, pierce is just the module's way of asking "Do I keep the bullet going, or do I stop it here?"
	-- You can make use of this unique behavior in a manner like this, for instance, which causes bullets to be bouncy.
	local position = raycastResult.Position
	local normal = raycastResult.Normal
	
	local newNormal = Reflect(normal, segmentVelocity.Unit)
	cast:SetVelocity(newNormal * segmentVelocity.Magnitude)
	
	-- It's super important that we set the cast's position to the ray hit position. Remember: When a pierce is successful, it increments the ray forward by one increment.
	-- If we don't do this, it'll actually start the bounce effect one segment *after* it continues through the object, which for thin walls, can cause the bullet to almost get stuck in the wall.
	cast:SetPosition(position)
	
	-- Generally speaking, if you plan to do any velocity modifications to the bullet at all, you should use the line above to reset the position to where it was when the pierce was registered.
end
1 Like

It depends on what kind of ricochet you want. You can adjust the Reflect function to change the direction of the ricochet.

Assuming that you want it to feel immersive and realistic, I would apply a little bit of randomness to it and maybe also tweak the velocity of the ricochet.

1 Like

Damn, I just realized I could make the value smaller… I kept trying to make the value larger.
Haha…

Thanks, @Brickman808

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