Raycast resulting in weird and wacky results?


For some reason, my bullets seem to go incredible off when shooting through these doors, or when the tip of your gun is inside a wall. The bullets should ignore your teams door, which it does, but I get really weird results on the raycast, where it goes backwards and all that?

You can see bullets being shot backwards, even tho I am aiming in the same spot every single time. Even when my gun isn’t in the door, the rays act weirdly, and don’t actually land where they should.

local SPEED = Config:GetAttribute("Speed")
local GRAVITY = Config:GetAttribute("Gravity")
	
CastParams.FilterDescendantsInstances = {
	Camera,
	player.Character, 
	Splatter,
	Bullets,
	workspace.Map:GetChildren()[1].TeamDoors[Player.Team.Name]
}

if GameSettings:GetAttribute("Teams") then -- Team based gamemode
	CastParams.CollisionGroup = player.Team.Name == "Blue" and "BluePaint" or "RedPaint"
end

-- Set up cast behavior
local CastBehavior = FastCast.newBehavior()
CastBehavior.RaycastParams = CastParams
CastBehavior.Acceleration = Vector3.new(0, -GRAVITY, 0)
CastBehavior.CosmeticBulletContainer = Bullets
CastBehavior.CosmeticBulletProvider = BulletCache

-- Projectile math
local Origin = firePoint
local Direction = (mousePosition - Origin).Unit

local ActiveCast = Caster:Fire(Origin, Direction, SPEED, CastBehavior) -- Fire shot

Note, I am using FastCast. I would post this problem in that thread, however I’ve posted several problems there already and gotten no response, so figured here would be better to ask

It looks like your mouse is hitting the invisible door. The offset between your mouse position and the gun barrel causes the discrepancy. Do you need an invisible door? You might need to use a camera ray that ignores transparent parts to find the actual mouse position. But so long as you are using mouse position in your equation, it will always get wonky in close proximity to walls and players and stuff.

I’m putting the door as part of the raycast params ignore descendants, so it should completely ignore these parts

So you are telling me that you are indeed using raycast to find the mouse position?

I won’t use Mouse.Position to find where to raycast to. Just use the character’s lookvector as a the UnitVector to raycast to.

local Origin = Character.PrimaryPart.CFrame
local Direction = Character.PrimaryPart.CFrame.LookVector

Make sure to multiply the vector by a number to make it go a distance

I think it has been mentioned already but I suggest you could use a raycast to find the position instead of mouse.

I think this is the problem.

The way you can get around this is using the Mouse | Roblox Creator Documentation property or use a separate Raycast with ignores the door.

You might still get the problem if you try clipping your gun through a wall too.

2 Likes

What I used instead of mouse.hit.p is this; I used this in a tutorial I made since the mouse will hit something I want to ignore.

local mouse = UserInputService:GetMouseLocation() or player:GetMouse() 
local unitray = camera:ScreenPointToRay(mouse.X , mouse.Y)