Does anyone know how to make it so that this "laser raycast" "strectches" itself in size from a part's position to a Mouse.Hit.Position?



In this screenshot, I used RayResults.Distance to stretch the Laser’s length but I also want it to strech the laser’s length to a Mouse.Hit.Position if the player misses. How do I do that?

1 Like

It’s hard to tell how your system proceeds the event in which no intersection is made with the raycast. Your issue, however, stems from the fact that you intend to draw the lazer from the muzzle of the firearm to that very intersection point. Despite the absence of an intersection, it is still possible to know the end position of the cast ray. We use the same calculation that Workspace:Raycast does:

destination = origin + direction

Altogether:

local intersection: Vector3

local origin = -- ...
local direction = -- ...
local result = workspace:Raycast(origin, direction, raycastParameters)

if result then
    intersection = result.Position
else
    intersection = origin + direction
end

-- Draw lazer

FYI

Your task.spawn is redundant; nothing in the passed function yields

1 Like

Wait the same calculation Workspace:Raycast uses is origin + direction to get the distance?

1 Like

It’s not calculating distance. It’s calculating the endpoint of the ray, so that a ray can be draw between that and the origin. It’s more convenient to work in terms of direction than endpoint, so that is why the function doesn’t explicitly take an endpoint

1 Like

Oh! I think that will work similarly right…?

1 Like

I’m not sure what you’re referring to. Either way, the solution I gave to you is designed to solve your problem

1 Like

I’m trying to find another way to get the Raycast’s distance by without raycasting (player missed target) and make a line that goes to their Mouse.Hit.Position


I tried doing your solution but when I shot at the air, it didn’t create a beam within 500 units but nothing

1 Like

You’re giving the client too much power. You should only be informing the server of the ray’s intersection position. The origin is assumed, and the distance calculated. I’ve already given you a way to calculate the anticipated intersection, so refactor your code to follow this new regime under that advice.

FYI:

task.wait(0.2)
Shootingdebounce = false

The above code appears at the end of all your if-statement’s clauses. This is code duplication that can be eliminated. You can simply move the code immediately after and outside the if-statement

1 Like

The origin isn’t assumed? I fire the origin (bulletraycast’s position) to the server using the remote event and thanks for the tip though I forgot about that!

1 Like

The origin is assumed by the server. You will always know that the origin should be the exit of the firearm’s muzzle. This is something that the server can enforce, rather than trust the client to agree with

Wait what?? I thougt the origin was supposed to be the starting point of the ray?

It is. I’m saying that the origin will always begin at BulletRayCastPart, which the server is aware of and can access on its own

I didn’t make a variable for BulletRayCastPart’s Position before the event was fired. I’m pretty sure it updates its position when the remote event is fired.

Regardless. Make sure that the client doesn’t tell the server where the ray starts. That needs to be done solely by the server. Do not forget to mark this specific reply as the solution

Is it because firing it to the server would be too slow to update?

It’s redundant and vulnerable to exploitation. The exit of the firearm’s muzzle replicates naturally. Add an attachment to make deriving its position easier

Okay, I’ll use that also when I shoot at the air (or just nothing) it still doesn’t create that “laser” from the origin

Then please send your current code so I can continue to diagnose the problem