- Home /
Custom Editor Snapping with Handles
Hi everyone, it's been some time now, and still didn't find any answer yet. I have a basic code that allows me to define block placement 'handles'. Here is the code for the Handle
public class BlockHandle
{
public Vector3 localPosition;
public Vector3 direction;
public string name;
}
So every 'Block' has an array of 'BlockHandle's, I can successfully retrieve them from a block to one another.
Here is how the handles look in prefab editing (note they are oriented, seed the red lines)
The problem is I can't get the blocks to snap with handles aligning properly.
This is the code I'm using so far
public static void SnapBlocks(Block movedBlock, BlockHandle movedHandle, Block destinationBlock, BlockHandle destinationHandle)
{
Vector3 worldDestAxle = destinationBlock.transform.TransformDirection(destinationHandle.direction);
movedBlock.transform.rotation = Quaternion.LookRotation(worldDestAxle);
Vector3 destPoint = destinationBlock.transform.TransformPoint(destinationHandle.localPosition);
Vector3 fromPoint = movedBlock.transform.TransformPoint(movedHandle.localPosition);
movedBlock.transform.Translate(destPoint - fromPoint, Space.World);
}
So far, it works well but for some orientations, there is a 180 degrees error, or when the handles are pointing upward, the blocks snap weirdly.
Thanks for reading til the end, and please help me out, it's driving me crazy !
Also, please let me know if I'm clear enough in my explanations.
Have a good day :)