Monday, March 31, 2014

unity3d - hit all objects on the screen or hit only one object on the screen click

Here is an example code:


//hit only 1 object
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitScreenObject;
if(Physics.Raycast(ray,out hitScreenObject))
{
if(Input.GetMouseButtonDown(0))
{
//isHit = false;
Destroy(hitScreenObject.collider.gameObject);
}
}

//hit all objects at the same time
RaycastHit2D[] hits;
//hits = Physics.RaycastAll(transform.position, transform.forward, 100.0F);
//Ray2D ray = Camera.main.ScreenPointToRay(Input.mousePosition, Vector2.zero);
                //Physics2D.raycastsHitTriggers = true;
               
                hits = Physics2D.RaycastAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
               
           


No comments:

Post a Comment