Attempting to make an angled raycast but not working as intended?
Basically, I'm trying to send three raycasts out from the character, each at a different angle. As you can see in this picture, one ray is supposed to go straight out which I can do with ease, but the outside rays are supposed to be angled and sometimes turn out wrong. You can see what I mean by turn out wrong in this gif. The third and fourth times the beams appear is what it's intended to look like, and all the other times are errors.
This is my code
01 | function rotatedVector (vector , theta) |
02 | local x = vector.X * math.cos(theta) - vector.Z * math.sin(theta) |
03 | local z = vector.X * math.sin(theta) + vector.Z * math.cos(theta) |
04 | return Vector 3. new(x , vector.Y , z) |
08 | local theta = math.rad( 30 ) |
09 | local lookVector 1 = char.HumanoidRootPart.CFrame.lookVector * 5 |
10 | local lookVector 2 = rotatedVector(lookVector 1 , theta) |
11 | local lookVector 3 = rotatedVector(lookVector 1 , -theta) |
21 | local ray = Ray.new(char.HumanoidRootPart.Position, rayEnd) |
22 | local part, position = workspace:FindPartOnRay(ray, char, false , true ) |
24 | local beam = Instance.new( "Part" , workspace) |
25 | beam.BrickColor = BrickColor.new( "Bright red" ) |
26 | beam.FormFactor = "Custom" |
27 | beam.Material = "Neon" |
31 | beam.CanCollide = false |
33 | local distance = maxDistance |
34 | beam.Size = Vector 3. new( 0.3 , 0.3 , distance) |
35 | beam.CFrame = CFrame.new(char.HumanoidRootPart.CFrame.p, position) * CFrame.new( 0 , 0 , -distance / 2 ) |
36 | game:GetService( "Debris" ):AddItem(beam, 1 ) |
Any help would be appreciated.