- Home /
Scripted Position Handle Not Locking Axis
I'm wanting to write a small 3d level editor. I just want to be able to add some features like shift+drag will fill in the tiles with whatever the current tile selection is and that type of stuff. I am still working on most of it, but was going to start with just dragging an item that would snap to a position. My idea was eventually I'd look at the potential position target then determine a grid cell it should be allocated too.
My issue is that when I grab an axis of the Position handle and drag it so the motion should only be confined to that axis sometimes I get small amounts of movement along another axis. This seems to happen roughly when my mouse passes over another axis even though my left click button hasn't came up yet. Unity objects in the scene don't have this same behavior. I've also noticed for Unity objects the other axes go grey when you click down on one, whereas mine become a less saturated version of what they normally are. Any idea what I am doing wrong? Edit: Found the solution you need to use Handles.FreeMoveHandle. Code is edited to work correctly.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Tile))]
public class TileEditor : Editor
{
Tile current;
private void OnEnable()
{
current = target as Tile;
}
private Vector3 SnapToGridPosition(Vector3 position)
{
position.x = Mathf.Round(position.x);
position.y = Mathf.Round(position.y);
position.z = Mathf.Round(position.z);
return position;
}
public void OnSceneGUI()
{
Vector3 result = Handles.FreeMoveHandle(current.transform.position, current.transform.rotation, HandleUtility.GetHandleSize(current.transform.position), Vector3.one, Handles.ArrowHandleCap);
current.transform.position = SnapToGridPosition(result);
}
}