Raycast bullets have no range and doesn't shoot when aiming at the sky

I’ve been working with raycasts recently more specifically with guns and I’ve ran into 2 small issues.

Issue #1: I’ve realized that no matter the range I set, my bullet length would be equal to where my mouse is.

Issue #2: If I aim towards the sky, it takes my ammo which means the ray is firing but the bullets don’t appear

If anyone knows how to fix this then your help would be greatly appreciated :+1:

2 Likes

Here is a video representation of the problem: https://medal.tv/clips/56744455/d1337nyO9zBr

Here is the script:

Bullets don’t appear when aiming at the sky because the ray hits nothing, so it’ll be nil. What I would do is to detect if there is a raycastResult and if it’s nil then simply just set the bullets to go to the direction, from the origin.

(Following your code, add an else statement and then set the bullet CFrame to the range, from the barrelPosition)

if raycastResult then

else
    bullet.CFrame = CFrame.new(barrelPosition,(mousePosition - barrelPosition).unit * RANGE)
    -- basically origin + direction
end
4 Likes

This makes so much sense and thank you for the concise explanation but I still end up a with a little bit of a problem. When I shoot the sky, the bullets appear but they do not position from the origin to the mouse position. This problem also happens when my mouse goes past the gun’s range that I have set.

Here’s a video representation: https://medal.tv/clips/56762792/d1337I41seLJ

1 Like

For the bullet, instead of cframe try testing position, set the position to Mouse.Hit.X, Mouse.Hit.Y, Mouse.Hit.Z

I’m not really sure what would cause it, you might need to debug it yourself or ask someone else or you can send file or code to me because somehow I am better at helping whenever I fix it myself by place file or script since I don’t know some CFrame expectations.

Anyway, I believe it’s a cframe problem or raycast. Also what is * CFrame.new(0,0,-distance/2)?

Sure I will send you the file in direct messages, also the * CFrame.new(0,0,-distance/2) is the midpoint of the barrel for the raycast im pretty sure.

An easy fix would be to use a module like FastCast instead of trying to make a shooting system from the ground up.

I’ve already look at and messed around with the fast cast module but the reason I am trying to make a gun system from ground up is to master raycasts.