vendredi 14 décembre 2012

Floor & Object picking


Routine de picking au sol simple et pratique:

static Plane floor = new Plane(Vector3.up, Vector3.zero);
static public bool TouchFloorPick(Vector2 touchPos, out Vector3 hitPoint)
{
float dist = 5000f;
Ray ray = mCam.ScreenPointToRay(new Vector3(touchPos.x, touchPos.y, 0));
float enter = 0.0f;
floor.Raycast(ray, out enter);
if (enter > 0.0f && enter <= dist)
{
hitPoint = ray.GetPoint(enter);
return true;
}
hitPoint = Vector3.zero;
return false;
}




Routine de picking sur object:


static public bool TouchObjectPick(Vector2 touchPos, out RaycastHit hit)
{
RaycastHit[] hits;
float dist = 50000f;

Ray ray = mCam.ScreenPointToRay(new Vector3(touchPos.x, touchPos.y, 0));
hits = Physics.RaycastAll(ray.origin, ray.direction, dist);
if (hits.Length>0)
{
hit = hits[0];
return true;
}
hit = new RaycastHit();
return false;
}

Aucun commentaire:

Enregistrer un commentaire