I’m experiencing inconsistency in the following code. I have a tool that fires a brick in the direction of the players mouse position. A local script passes the mouse’s position to the server and then the server uses the below code to fire the brick towards the mouse’s position, while also raycasting to detect any collisions.
local BEG = Part.CFrame.p
local DIR = (Pos-BEG).Unit*6 --Pos is the mouse's position
local raycastResult1 = workspace:Raycast(BEG,DIR,raycastParams)
--CommonModule.DrawRaycast(BEG,DIR)
local raycastResult2 = workspace:Raycast(Part.CFrame*CFrame.new(0,1,0).p,DIR,raycastParams)
--CommonModule.DrawRaycast(Part.CFrame*CFrame.new(0,1,0).p,DIR,raycastParams)
local INT = raycastResult and raycastResult.Position or BEG+DIR
local DIS = (BEG-INT).Magnitude
Part.CFrame = CFrame.new(BEG,INT)*CFrame.new(0,0,-DIS/2)
--Raycast hit detection comes next but doesn't affect the issue
The DrawRaycast function that is called is what I’m using to visualize the raycast. It’s code is as follows:
local MID = BEG+DIR/2
local Part = Instance.new("Part")
Part.Anchored = true
Part. CanCollide = false
Part.Transparency = .5
Part.Size = Vector3.new(1,1,DIR.Magnitude)
Part.CFrame = CFrame.new(MID,BEG)
Part.Parent = workspace
This is the result from the above code being looped:
On the first raycast, you can see raycast1 is centered and raycast2 is offset 1 stud above. The issue is every raycast2 after is in the same position as raycast1. What am I doing wrong here? Any suggestions would be appreciated!
Thanks!
Romul