I’m making code that automatically casts a ray to terrain, and then creates a crystal with a random width and size. The crystals spawn fine, but when I try to rotate them so the head of the tips are pointing away from the terrain, the closer they get to being upside-down, the further underground they get. I’m relatively new to using CFrames, so I’m not really sure what the problem is here.
These two crystals have spawned fine, they are mostly outside of the terrain.
This crystal, about 10-20 degrees above the horizon, partially spawned inside the cave wall.
is
This crystal, which is almost vertical with the tip facing up, is almost entirely inside the terrain.
Below is the code I’m using to calculate the position of the base of the crystal (the primary part), the tip is welded. I am using TypeScript to Lua, but this should be pretty easy to translate into Lua.
crystal.PrimaryPart.CFrame = new CFrame(result.Position, result.Position.add(result.Normal))
.mul(CFrame.Angles(math.rad(-90), 0, 0))
.add(new Vector3(0, -(chosenShaftLength / 2) + 1, 0));
Inverting the values on the last line has the opposite effect, whereas crystals in the floor spawn fine but the crystals in the ceiling are recessed pretty far.