- Home /
Collider and Distance Problem OnMouseDown
I'm having some problems with the onmousedown event on some of my collider objects.
Here's two screenshots illustrating the problem:
Screenshot A: http://i.imgur.com/2IWzQXR.png
Screenshot B: http://i.imgur.com/NoEplyB.png
Some background on the images: This is procedurally generated roller coaster track along a hermite spline curve via the control points you see in yellow connected via a line renderer that is also yellow.
Screenshot A shows the distance at which I would like to be able to click and drag the control point, however it doesn't work.
Screenshot B shows how close I have to be to the object to get it to work. Notably I have to clip through the track.
Here are some caveats: There is no collider currently enabled on the track! The button in the bottom left "ride it" is what enables the collider. Does having it not enabled mean I can't look "through" it as if it weren't there in the first place?
If I try to click/drag outside the track on the exposed parts of the handles I can do it a bit further away, but still not as far away as I would like.
I'm thinking it might be that it is somehow seeing gui components, but it does still work close up. I fiddled with the clipping planes for kicks and grins but that wasn't it either.
Again, it works at pretty close distances but not far away. It needs to work far away. Below is relevant code.
public class draggable : MonoBehaviour {
Vector3 screenPoint, offset, scanPos;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
scanPos = transform.position;
}
void OnMouseDown()
{
print ("I see you");
screenPoint = Camera.main.WorldToScreenPoint(scanPos);
offset = scanPos - Camera.main.ScreenToWorldPoint(
new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}
}
This code listens for a toggled bool in my gui script to disable and enable the colliders. This code lies on the gameobject that procedurally generates the roller coaster track.
if(guiobj.riding) {
collider.enabled = true;
}
else {
collider.enabled = false;
}
Answer by Wuzseen · Jun 26, 2013 at 04:13 PM
So I've been fiddling with this for hours now and I opted for a solution using raycasts.
A script residing on my camera looks if a ray collides with one of these draggable objects via a tag. Moreover, I moved the ramp to the ignore raycast layer.
Things are working perfectly now.
Your answer
![](https://koobas.hobune.stream/wayback/20220613104913im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Mouse drag with specific angle 1 Answer
Internal collisions 1 Answer
"Show GUITexture within distance" issue 0 Answers
How to use Sphere colliders to detect the "Player" 1 Answer
2D - Overlaying Colliders - MouseDown not detected 0 Answers