How do I optimize this OOP code to my likings?

  1. What do you want to achieve?

I am currently in the process of working on a completely custom ray system.

There is one problem though.

  1. What is the issue?

I currently have code in a for loop which changes the size as the index, and the position relative to the size.

CAST.Size += Vector3.new(i, 0, 0)

CAST.Position = Vector3.new(origin.X + CAST.Size.X / 2, origin.Y, origin.Z)

This works, as long as my part isn’t orientated on the Z or Y axis. Now, I know why this is happening, though I don’t know a way to fix it.

Me not orientating the part (X Axis):

VS

Me Orientating the part (Z Axis):

As you can see, there is quite a difference.

  1. What solutions have you tried so far?

I’ve looked everywhere with ideal information (to me).

Maybe use CFrame instead of position? I don’t see the part of your code that tells the angle, so I’ll just make this:

local direction = Vector3.new(0, 0, i)
CAST.Size += direction
CAST.CFrame = CFrame.lookAt(origin, origin + direction) + direction / 2
1 Like

Well that doesn’t work.

I’m trying to make the ‘CAST’ part look at the origin’s angle, and size is forward from there.

And I mean X, Y, or Z angle.

Not just X like I have.

local p1 = workspace.Start
local p2 = workspace.End
local origin = p1.Position
local direction = p2.Position - p1.Position

local part = Instance.new("Part")
part.Anchored = true
part.CanCollide = false
part.Size = Vector3.new(0.25, 0.25, direction.Magnitude)
part.CFrame = CFrame.lookAt(origin + direction / 2, origin + direction)
part.Parent = workspace

So the important bits are here:

CAST.Size = Vector3.new(0.25, 0.25, direction.Magnitude)
CAST.CFrame = CFrame.lookAt(origin + direction / 2, origin + direction)

CFrame does rotate the part so if you don’t want that to happen, then you have to use something else, like FaceNormals or something.

local direction = Vector3.new(-1, 0, 1)
local size = Vector3.new(math.abs(direction.X), math.abs(direction.Y), math.abs(direction.Z))
local cframe = CFrame.new(direction / 2)
CAST.Size += size
CAST.Position *= cframe