- Home /
Get object hovered over by touch
How can i get the object in 3D space touched, but not necessarily touch ended over. If the touch input starts at point A and ends at point D, and passes over points B and C, i want to get the point where the touch is held. In this case it will be point B and then point C. The touch will not necessarily be stationary/moving. It can be either. I want the effect of a slider, where the slider thumb will move to to object the over which the finger is held/moved.
I hope i could explain what i need.
I think I'm confused, could you provide a diagramatic explanation or something to make it more clear?
Paint works, too. :D
I wonder why i didnt get an email notification. anyway:
This is the script im using :
for (var touch : Touch in Input.touches) {
if(touch.phase == TouchPhase.Ended){
if (Input.touchCount == 1) {
var ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)){
if(hit.collider.transform.parent.gameObject.name == "Sensitivity"){
sensitivity = parseFloat(hit.collider.transform.name);
GameObject.Find("Sensitivity$$anonymous$$nob").transform.localPosition.x = 2.175 - sensitivity * 0.375;
}
}
}
}
}
What is happening now is that the Thumb(the radioactive icon) is just moving to the collider on which the touch input ends. i want it to be on the collider touched while sliding. (like the android volume slider)
Answer by supernat · Jul 03, 2014 at 05:12 PM
I'm thinking out loud here, but you probably want to do this in two steps. First, find out all objects that lie within the plane from point A to point B, where point A is the left side of the slider here and point B is the right side. This could be done by creating a cone with its apex at the camera and its diameter the length of the slider, and its direction vector from the camera to the center point of the slider. Then you can test each collider in the scene (I would use layers additionally to limit the number of tests) as lying within that cone (simple dot product test). If your architecture makes it possible that other objects/colliders would fall within the cone above or below these colliders, then you would need to work with planes instead.
In step two, find out which object the current touch position is closest to by checking the distance between finger position and all object positions and keeping a reference to the object with the smallest distance. I would probably do all of those tests in screen space to make it easier. When your finger is between two colliders, the thumb will probably jump back and forth rapidly, so you'll also want some hysteresis in there.
Thanks a lot. I think my case is much simpler, so i can do this more easily. I just figured it out :) Thanks
Your answer
![](https://koobas.hobune.stream/wayback/20220613153113im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity 2D Touch Controll via on screen button for Mobile 0 Answers
Touch inputs Angry-bird style 0 Answers
How to detect whether a particular touch has ended? 1 Answer
Input.GetAxis("Vertical") on touch devices. 2 Answers
Help with a button 1 Answer