I want to make a raycast from this part in the image above in the direction of it’s lookvector.
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.
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
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
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
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:
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!