Cframe.Angles are Weird

  1. What do you want to achieve?
    I wanted to test out a melee hitreg system. It would work by firing a ray in front of the player’s character, then firing rays at a 90 degree angle.

  2. What is the issue?
    The issue is that one raycast is different from the other raycast. I have no idea what causes this.

  3. What solutions have you tried so far?
    I tried changing Cframe.new into Cframe.Angles, but it didn’t work. I tried removing the .unit at the basedirection, but it was really weird.

local angles									= {math.rad(90), math.rad(1), math.rad(-90)}

local function makeRayVisible(origin: Vector3, direction: Vector3)
	local radius = .1
	local midpoint = origin + direction/2

	local visibleRay = Instance.new("Part", workspace)
	visibleRay.Anchored = true
	visibleRay.CFrame = CFrame.lookAt(midpoint, origin)
	visibleRay.Size = Vector3.new(radius, radius, direction.Magnitude)
	visibleRay.Material = Enum.Material.Neon
	visibleRay.BrickColor = BrickColor.Green()
	visibleRay.CanCollide = false
	visibleRay.CanQuery = false
	
	--Debris:AddItem(visibleRay, 5)

	return visibleRay
end

local origin = part.Position + offset
local baseDirection = CFrame.new(origin).LookVector.Unit*range

for _, degrees: number in pairs(angles) do
	local direction = CFrame.new(baseDirection)*CFrame.new(degrees, math.rad(0), degrees)
	local newDir = direction.Position.Unit*range
					
	local result = workspace:Raycast(origin, newDir, params)
	makeRayVisible(origin, newDir)
end
2 Likes

something you should be wary of is that CFrame.Angles only takes arguments in radians

it’s very easy to convert degrees into radians, just do math.rad(degree)

2 Likes

image
Currently, you are rotating it along two axis, the X and the Z axis (by the same amount on both). I assume what you are trying to achieve is a rotation along a single axis, which would look like this

... * CFrame.Angles(0,degrees,0)

(and this line)
image
You should also probably change this to (so it does makes a ray at 0° instead of 1°)

{math.rad(90), 0, math.rad(-90)}

Also, CFrame.new and CFrame.Angles are completely different. CFrame.new() creates a CFrame with no orientation while CFrame.Angles() creates a CFrame with no position. In your case, you want to rotate it, so you want a CFrame with an orientation

1 Like


For some reason, the three rays only shoot out foward

local angles									= {math.rad(90), math.rad(0), math.rad(-90)}

local origin = part.Position + offset
local baseDirection = CFrame.new(origin).LookVector.Unit*range

for _, degrees: number in pairs(angles) do
	local direction = CFrame.new(baseDirection)*CFrame.Angles(0, degrees, 0)
	local newDir = direction.Position.Unit*range
					
	local result = workspace:Raycast(origin, newDir, params)
	makeRayVisible(origin, newDir)
end

My bad,

image

Try this:

local direction = CFrame.Angles(0, degrees, 0) * CFrame.new(baseDirection)

This should now properly rotate the baseDirection. Before, I am pretty sure it was rotating the CFrame after it was extended by baseDirection (which means the position didn’t change), but now it should extend baseDirection relative to the orientation… CFrames can be confusing lol

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.