I am populating 3d markers on my map. Next goal is to detect which marker is being clicked. attached UI button with each marker but that is not a good way. can anyone suggest something better?
Asked
Active
Viewed 8,022 times
0
-
While Raycast should work, Unity's [EventSystem](https://docs.unity3d.com/ScriptReference/EventSystems.EventSystem.html) should be used for detecting clicks. This will remove some problems you will encounter later on in your code. Check how to do this on the duplciated answer where it says *"**5**.For 3D Object (Mesh Renderer/any 3D Collider)"* – Programmer Mar 06 '17 at 07:17
2 Answers
1
Does your markers have a collider? If thats the case then you can use you can use the OnMouseDown
function in a script component attached to your game object, as long as it has a collider component.
Otherwise you can use this snippet from http://answers.unity3d.com/questions/34795/how-to-perform-a-mouse-click-on-game-object.html
function Update ()
{
if ( Input.GetMouseButtonDown(0))
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit, 100.0))
{
// handle mouseclick
}
}
}

uzr
- 1,210
- 1
- 13
- 26
-
yes, i have a collider with the gameobject. On mouse down did the job.. thanks.. :) – sristy_poka Mar 06 '17 at 07:29
0
You can attach a collider in gameobject and detect with Physic.RayCast :
https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

Cổ Chí Tâm
- 330
- 1
- 7
-
Please elaborate on your answer and summarize the link in case it ever dies out. – Hazel へいぜる Jul 11 '21 at 22:32