Search Unity

Discussion What is the best strategy to create a mouse drag selection in 3d space ?

Discussion in 'Scripting' started by Draad, May 8, 2023.

  1. Draad

    Draad

    Joined:
    Feb 17, 2011
    Posts:
    325
    Hello,

    I am working on a voxel editor. My goal is to be able to :
    -> click = select a voxel
    -> drag = extend voxel selection
    -> release = confirm selection
    -> user can rotate the object he is editing to see it from any angle.

    Now, in order to extend user's selection, I am trying to interpret mouse mouvement in UI 2d space into World 3d space, and I am wondering what strategy would you adopt to achieve this ?

    For now, I have two ideas.

    Idea 1
    a - Detect click from a collider
    b - Consider an imaginary plane where pivot point is center of the voxel clicked
    c - Every time mouse move :
    --> Convert mouse position into viewport position
    --> Cast a Ray from camera, if it does not hit the first collider :
    ------> Find point that intersect the imaginary plane
    ------> Use this point in space to know where to extend / decrease my selection
    ------> Return to b
    test1.png

    Idea 2 :
    a - Detect click from a collider
    b - For each face of the cube on axis X and Z :
    --> Draw an imaginary triangle that start from collider pivot point and interect face's edges
    --> Convert this imaginary triangle in a UI zone
    c- Every time mouse move :
    --> Convert mouse position into viewport position
    --> Cast a Ray from camera and find point that intersect the imaginary plane
    --> Use this point in space to know where to extend / decrease my selection
    --> Return to b

    test2.png
    Do you think it is a good strategy ? Is there any other way you would suggest ?
    Thanks