Using a ray, in a raycast, with out hitInfo and a layer mask.

Hello,

I’m needing to use a ray (ScreenPointToRay) and output the hitinfo, as well as use a layer mask. Yet, there is no overload that allows me to do this. How should I go about this?

Or, How do I use a ray in a raycastall, and output the hit info?

I believe you’d use something like

hit : RaycastHit;

and then from there you could use something like

hit.transform.position = curHitPosOrWhateverYouWantToDoWithThePosition; 
//or if you want the rigidbody data
hit.rigidbody.velocity = whateverVelocityYouWant;
//or if you want the distance from the raycast's origin point
hit.distance = curHitDistOrWhatever;

basically RaycastHit is a variable (gameObject) which you can use to output whatever data you want.

That’s fantastic, but in no way related to my issue. First off, C#, and second, I can raycast, I know how they work, etc, but there is an overload in the raycast function that I need, and does not exist.

What my raycast needs to do is:

  1. Use a predefined Ray.
  2. Use layermasks
  3. Output a RayCastHit
private Ray ray;
private RaycastHit hit;
private Collider myCollider = new Collider();
private LayerMask layerMask = 1 << 11;


// then in your loop somewhere
if (Physics.Raycast(ray, out hit, 110, layerMask))
{
	myCollider = hit.collider; // Grab the selected collider
}
// For your predefined ray, something like this - if you want mouse position that is
ray = Camera.main.ScreenPointToRay(Input.mousePosition);

You should also be able to add a float for the distance and add distance = hit.distance; etc.

The above is just copied from my code so you will have to rename variables, increase the distance, remove the myCollider if you don’t need it etc. It should get you going.

So what do you call this?

static function Raycast (ray : Ray, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool

Using-a-ray-in-a-raycast-with-out-hitInfo-and-a-layer-mask

Sorry, just a bit unclear to me from your first post! :stuck_out_tongue: