Search Unity

Cahnge value of 3D Slider module with mouse

Discussion in 'Scripting' started by vicou96, Oct 22, 2019.

  1. vicou96

    vicou96

    Joined:
    Oct 16, 2019
    Posts:
    3
    Hi all,
    I'm currently trying to implement a 3D slider module in order to control a value.
    My model currently look as attached. fader.png
    To move the cursor with my mouse, i used the drag code from this tutorial :

    Then, to avoid getting the cursor out of the model, I placed an invisible cube (in green on the picture) that look like a bar (thin as a plane) to put the cursor on.
    To get a valid position on the bar for the cursor, the bar have a box collider. I can then use the function
    Code (CSharp):
    1. ClosestPoint(mousepoint);
    .
    This technique is working almost as expected, however, when i place the full model orienting toward me. faderTowardsMe.png
    And that I place the cursor at the minimum, I should normally attain the
    Code (CSharp):
    1. Collider.bounds.min
    position, but instead, the z value is nether reached (here there is 3.0 of difference), even if on the screen, the cursor appear to be at the right position (bottom of model).
    Debug of my min point in the collider and the calculated point with the ClosestPoint function
    Point min(-0.5, -0.7, -4.1) Calculated(-0.5, -0.7, -0.5)
    This is a problem for me as I want to calculate a value of the slider given the position of the cursor in the bar.

    My question is: Why does this value z is nether attained Or: Do you know some way to achieve this differently?
     

    Attached Files:

  2. cmyd

    cmyd

    Joined:
    Oct 29, 2017
    Posts:
    98
    Post your code as well.
     
  3. vicou96

    vicou96

    Joined:
    Oct 16, 2019
    Posts:
    3
    @cmyd my code so far
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TobiasErichsen.teVirtualMIDI.test;
    5.  
    6.  
    7. public class ButtonBehaviour : MonoBehaviour
    8. {
    9.     // Start is called before the first frame update
    10.     private Transform parent;
    11.     private Collider col1;
    12.     private Transform cube;
    13.     void Start()
    14.     {
    15.         parent = gameObject.transform.parent;
    16.         cube = parent.Find("Cube");
    17.  
    18.         col1 = cube.GetComponent<Collider>();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.      
    25.     }
    26.  
    27.     private Vector3 mOffset;
    28.  
    29.  
    30.  
    31.     private float mZCoord;
    32.  
    33.  
    34.  
    35.     void OnMouseDown()
    36.  
    37.     {
    38.      
    39.         Debug.Log("OnmousedownButton");
    40.         mZCoord = Camera.main.WorldToScreenPoint(
    41.  
    42.             gameObject.transform.position).z;
    43.  
    44.      
    45.  
    46.         // Store offset = gameobject world pos - mouse world pos
    47.  
    48.         mOffset = gameObject.transform.position - GetMouseAsWorldPoint();
    49.  
    50.     }
    51.  
    52.  
    53.  
    54.     private Vector3 GetMouseAsWorldPoint()
    55.  
    56.     {
    57.  
    58.         // Pixel coordinates of mouse (x,y)
    59.  
    60.         Vector3 mousePoint = Input.mousePosition;
    61.  
    62.  
    63.  
    64.         // z coordinate of game object on screen
    65.  
    66.         mousePoint.z = mZCoord;
    67.  
    68.  
    69.  
    70.         // Convert it to world points
    71.  
    72.         return Camera.main.ScreenToWorldPoint(mousePoint);
    73.  
    74.     }
    75.  
    76.     void OnMouseDrag()
    77.  
    78.     {
    79.  
    80.         Vector3 mousepoint = GetMouseAsWorldPoint();
    81.         /*mousepoint.z = mOffset.z;*/
    82.      
    83.        
    84.  
    85.         gameObject.transform.position = col1.ClosestPoint(mousepoint);
    86.         Debug.Log(col1.bounds.center - col1.bounds.extents);
    87.         Debug.Log("Point min"+col1.bounds.min+" Calc:"+col1.ClosestPoint(mousepoint));
    88.        
    89.  
    90.     }
    91.  
    92. }
    93.  
     
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Take a look at the Steam VR plugin, they have a 3d slider in there which could be adapted to work with a mouse without too much bother.
     
  5. vicou96

    vicou96

    Joined:
    Oct 16, 2019
    Posts:
    3
    @WarmedxMints Thanks for the tip! Can't seem to find it in steamVR but i'll look deeper, also, could it not work by using the default Unity UI slider and changing the "handle" image to a 3D model?
     
  6. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    It's in the examples. Take a look at the example scene. The UI slider class won't do the job. You are looking to calculate the slider value at a point between two vectors.