Question by
yade_123 · Nov 09, 2016 at 01:51 PM ·
c#raycastarray of gameobjects
how to create an array of gameobjects by using raycast?
Hi, how can I use raycast to create an array of gameobjects to access later on? Something like this-
using UnityEngine;
using System.Collections;
public class transformation : MonoBehaviour {
public LayerMask touchinputmask;
public GameObject [] objects;
void Start () {
}
void Update () {
Ray r = new Ray(transform.position,Vector3.Up,100,touchinputmask);
RaycastHit[] hits = Physics.RaycastAll(r,100,touchinputmask);
foreach(RaycastHit hit in hits)
{
objects=hit.collider.tag;
}
}
}
Comment
Best Answer
Answer by doublemax · Nov 09, 2016 at 02:49 PM
Untested, but you should get the idea:
using UnityEngine;
using System.Collections;
public class transformation : MonoBehaviour
{
public LayerMask touchinputmask;
public GameObject [] objects;
void Update ()
{
RaycastHit[] hits = Physics.RaycastAll(transform.position, Vector3.up, 100.0f, touchinputmask);
if( hits.Length > 0 )
{
objects = new GameObject[ hits.Length ];
for( int i=0; i<hits.Length; i++ )
{
objects[i] = hits[i].collider.gameObject;
}
}
}
}
Your answer
