- Home /
How to add onclick event on sphere 3d object?
I have a sphere 3d object and i want to load new scene if user taps or clicks. I have seen tutorials relating to the button, but i want it to be done by user click or tap on sphere objects.
If it is possible please suggest appropriate solution. Thanks in advance.
Answer by efeguclu · Jan 12, 2018 at 02:02 PM
Use OnMouseDown in a script attached to sphere
 //Triggered when you click on the sphere
 private void OnMouseDown(){
 //Do something
 }
It worked after adding collider. I want to know is this method used for all platforms i.e. Desktop or mobiles?
Answer by Brogan89 · Jan 13, 2018 at 07:08 AM
If you want to support both platforms I would do something like this
         Vector3 clickPos;
         bool clicked;
 
 #if UNITY_ANDROID || UNITY_IOS
         clicked = Input.touchCount > 0;
         if(clicked)
             clickPos = Input.GetTouch(0).position;
 #else
         clicked = Input.GetMouseButtonDown(0);
         clickPos = Input.mousePosition;
 #endif
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(clickPos);
         if (Physics.Raycast(ray, out hit) && clicked)
         {
             Debug.Log("Clicked on gameobject: " +  hit.collider.name);
         }
I haven't tested this, so don't take it as gospel.
I found On$$anonymous$$ouseDown method is working fine on android. I guess it will work for IOS devices too. Can you confirm this?
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                