Help With Raycasting

  1. What do you want to achieve? Keep it simple and clear!

    I want to make a raycast from this part in the image above in the direction of it’s lookvector.
  2. What is the issue? Include screenshots / videos if possible!
    When playing the game, the raycast doesn’t start from the position of the part but when I pause the game, it works just fine.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried this:
local function onRenderStepped(dt)
	if isMouseButton1Down then
		local part = gun.Fire
		local rayOrigin = part.Position
		local rayDirection = part.CFrame.LookVector * 100
		local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
		if raycastResult then
			print("Ray Fired")
			local distance = (rayOrigin - raycastResult.Position).Magnitude
			local p = Instance.new("Part")
			p.Anchored = true
			p.CanCollide = false
			p.Size = Vector3.new(0.1, 0.1, distance)
			p.CFrame = CFrame.new(rayOrigin, raycastResult.Position)*CFrame.new(0, 0, -distance/2)
			p.Parent = workspace
		end	
	end
end

If you multiply the look vector by 100 the direction goes way off… don’t do that. And why are you multiplying it in the first place?

No, that is completely wrong. It’s basically just multiplying the length of the vector by 100.

3 Likes

You are multiplying direction not the position, direction and position are different. Suppose if the direction is 25,90,36 multiplying it by 100 will go 250,900,360 which is wrong

Try not multiplying the vector and see what happens

Try plugging in the vectors. Both will have the same direction, except 1 is longer than the other.

1 Like

@AkariInsko try it I bet that will work

I multiplied the lookvector to make the raycast go 100 studs in the direction of the part’s lookvector.

Just remove it and try its like 5 characters what’s bad in trying

All that changed was the length of the raycast.

but did it work? did it go in the direction you wanted it to?

It was almost the same as when I multiplied the lookvector but only the length of the raycast changed. (Yes, it didn’t work)

How bout this?
ray direction = mouse.Hit.p - ray origin

That was what I used before, but I want to raycast in the direction of the gun’s barrel, not where the mouse is pointing.

Oh and also you need to put in raycast params because the raycast would just hit the gun model itself

1 Like

Just tried your code and it works as expected. The ray is probably hitting the gun’s model like @GiantDefender427 said try making some RayCastParams like this:

local rayCastParams = RaycastParams.new()
rayCastParams.FilterDescendantsInstances = {gunModel} -- Where ever your gun model is located
rayCastParams.FilterType = Enum.RaycastFilterType.Blacklist

and then apply the RaycastParams to your ray:

local raycastResult = workspace:Raycast(rayOrigin, rayDirection, rayCastParams) -- 3rd argument is the RaycastParams
2 Likes

This is incorrect. Multiplying a vector value by a scalar value will amplify the magnitude and leave the direction entirely the same. This is true for all vector*scalar multiplication, even that which takes place in a 2 dimensional space. This is fundamental vector math.

Here is a Khan Academy video that further explains the concept:

1 Like

That means part of the simulation is effecting the raycast position and such like the guns velocity or gravity or anything physics based.

Make sure the gun is anchored to prevent this from occuring.

Like this

1 Like

The raycasting works now. I dont really know what made it not work but I think it had something to do with weld constraints. Thanks for responding everyone!