0
\$\begingroup\$

I have a vector3 value that I am setting in inspector and I am drawing a ray using OnDrawGizoms to draw that vector line.

However, the position handle I made using Handles API to set the vector3 value is placed certain distance away from the vector unit. It's not placed exactly at the end point of the vector line that is drawn and I am not sure why.

My goal is to just make a handle to position a vector3 value.

EDIT: For some reason, the handle is positioned correctly for y position alone but not for x and z positions.

Here is my code:

In this script, it is the localTargetPosition vector3 value that I want to make my handle for.

public class MovePlatform : MonoBehaviour
{
public Vector3 localTargetPosition = new Vector3(5, 0, 0);
public Vector3 m_initialPosition { get { return initialPosition; } set { initialPosition = value; } }
private Vector3 initialPosition, targetPosition, targetPosDebug;
public float speed = 3;
public bool loop;

void Start()
{
    initialPosition = transform.position;
    SetTargetPosition(localTargetPosition);
}

void Update()
{
    transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);

    if (transform.position == targetPosition)
    {
        if (loop)
        {
            SwitchDirection();
        }
    }
}

public void SwitchDirection()
{
    if (transform.position == initialPosition)
    {
        SetTargetPosition(localTargetPosition);
    }
    else
    {
        targetPosition = initialPosition;
    }
}

private void SetTargetPosition(Vector3 localPosition)
{
    targetPosition = initialPosition + transform.TransformDirection(localPosition);
    targetPosDebug = targetPosition;
}

void OnDrawGizmos()
{
    if (Application.isPlaying)
    {
        Debug.DrawLine(initialPosition, targetPosDebug, Color.red);
    }
    else
    {
        Debug.DrawRay(transform.position, transform.TransformDirection(localTargetPosition), Color.red);
    }
}
}

This is the script used for making handle:

[CustomEditor(typeof(MovePlatform)), CanEditMultipleObjects]
public class MovePlatformEditor : Editor
{
protected virtual void OnSceneGUI()
{
    MovePlatform movePlatform = (MovePlatform)target;

    Vector3 initialPosition = movePlatform.transform.TransformPoint(movePlatform.m_initialPosition);
    Vector3 targetPosition = movePlatform.transform.TransformPoint(movePlatform.localTargetPosition);

    EditorGUI.BeginChangeCheck();
    Handles.Label(targetPosition, "Target", "button");
    targetPosition = Handles.PositionHandle(targetPosition, movePlatform.transform.rotation);

    if (EditorGUI.EndChangeCheck())
    {
        Undo.RecordObject(movePlatform, "Move Handles");

        movePlatform.localTargetPosition = movePlatform.transform.InverseTransformPoint(targetPosition);
    }

    Handles.color = Color.yellow;
    //Handles.DrawLine(initialPosition, targetPosition);
}

}
\$\endgroup\$
  • \$\begingroup\$ Possibly a UX issue so the handle doesn't hide/cover the actual point? \$\endgroup\$ – Patrick Hughes Mar 8 '19 at 21:44
  • \$\begingroup\$ I figured out the problem. So I have the MovePlatform script attached to the cube object which is scaled by 4 units in x and z but just by 1 unit in y. If I reduce the scale of cube in x and z to 1, it positions the handle correctly but then I lose the scaling of cube which I need to make it look like a platform. So not sure how to go about fixing it. \$\endgroup\$ – AaySquare Mar 9 '19 at 14:51
0
\$\begingroup\$

I fixed my own problem. All I had to do was attach the MovePlatform script to an empty game object and make the cube that I wanted to move as the child of this game object. The problem was I originally had the script attached to the cube itself and because the cube's scale was not (1,1,1) but (4,1,4), it positioned the handle further away in x and z positions by approx 40 units.

\$\endgroup\$
  • \$\begingroup\$ You might have meant to use TransformPoint or Transform Vector instead. These methods take scale into account, while TransformDirection uses only rotation. \$\endgroup\$ – DMGregory Mar 9 '19 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.