#help with bullet going backwards

1 messages · Page 1 of 1 (latest)

hardy iris
#
GunHandler.cast = function(gun, endPos: Vector3, force)
    local BulletSpawn = gun.BulletSpawn
    
    local Bullet = RepStorage.Bullets:FindFirstChild("Bullet"):Clone()
    local spawnPos = BulletSpawn.Position
    local direction = (endPos - spawnPos).Unit
    local forward = BulletSpawn.CFrame.LookVector

    -- is bullet pointing backwards
    if direction:Dot(forward) < 0 then
        direction = forward
    end

    Bullet.CFrame = CFrame.new(spawnPos, spawnPos + direction)
    Bullet.Parent = workspace.Bullets
    
    Bullets[Bullet] = {
        TimeElapsed = 0,
        StartPos = Bullet.Position,
        Gun = gun,
        Force = force,
        EndPos = spawnPos + direction * 1000,
        DespawnTime = 4
    }
end
``` (endpos is mouse.hit)

so the bullet spawns at the tip of the guns barrel but sometimes the gun goes inside a wall or something and the barrel becomes IN FRONT of mouse.hit causing the bullet to go backwards, how do i fix this? the dot doesnt work
pseudo zenith
#

you can add a decal on the front of your spawner object if you need to see it

hardy iris
pseudo zenith
#

im assuming its not an issue with your calculations since i didnt fully read them, if that doesnt work ill look at your calculationsa bit more

hardy iris
#

the bullet shoots fine except that when the gun goes inside a wall or inside the floor

pseudo zenith
#

just raycast then

#

or wait

hardy iris
#

what

pseudo zenith
#

oh its probably

#

wait lemme read this again

hardy iris
#

i just added 10 studs onto the mouse.hit

#

made it 10 studs in front

pseudo zenith
#

oh you fixed it?

hardy iris
#

yes

pseudo zenith
#

ok

#

i was gonna say to use a raycast and just check if theres a wall in front of the barrel