Hi all,
I’m currently trying to implement a 3D slider module in order to control a value.
My model currently look as attached.
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 ClosestPoint(mousepoint);
.
This technique is working almost as expected, however, when i place the full model orienting toward me.
And that I place the cursor at the minimum, I should normally attain the
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?
@cmyd my code so far
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TobiasErichsen.teVirtualMIDI.test;
public class ButtonBehaviour : MonoBehaviour
{
// Start is called before the first frame update
private Transform parent;
private Collider col1;
private Transform cube;
void Start()
{
parent = gameObject.transform.parent;
cube = parent.Find("Cube");
col1 = cube.GetComponent<Collider>();
}
// Update is called once per frame
void Update()
{
}
private Vector3 mOffset;
private float mZCoord;
void OnMouseDown()
{
Debug.Log("OnmousedownButton");
mZCoord = Camera.main.WorldToScreenPoint(
gameObject.transform.position).z;
// Store offset = gameobject world pos - mouse world pos
mOffset = gameObject.transform.position - GetMouseAsWorldPoint();
}
private Vector3 GetMouseAsWorldPoint()
{
// Pixel coordinates of mouse (x,y)
Vector3 mousePoint = Input.mousePosition;
// z coordinate of game object on screen
mousePoint.z = mZCoord;
// Convert it to world points
return Camera.main.ScreenToWorldPoint(mousePoint);
}
void OnMouseDrag()
{
Vector3 mousepoint = GetMouseAsWorldPoint();
/*mousepoint.z = mOffset.z;*/
gameObject.transform.position = col1.ClosestPoint(mousepoint);
Debug.Log(col1.bounds.center - col1.bounds.extents);
Debug.Log("Point min"+col1.bounds.min+" Calc:"+col1.ClosestPoint(mousepoint));
}
}
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.
@WarmedxMints_1 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?
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.