- Home /
problem with multiple raycasting from multiple gameobjects
hi to all,sorry if am wrong.iam trying to raycast from different gameobjects dynamically using forloop.sorry if am trying in a wrongway.please help me. my scenario is very simple that i need to check whether there is any game object infront of any car(gameobject from which raycast is produced).here, i have more than 5 cars.so i need to raycast from those 5cars and have to capture the hitpoints,here is my attempt of producing multiple raycast from multiple gameobject.plz help me.thanks in advance
Ray[] RaysArr;
RaycastHit[] hitArray;
public Gameobject[] cars;
void start()
{
cars= new GameObject[5]
for(int i =0; i<cars.Length;i++)
{
cars[i]= GameObject.Find("cars_0"+i);
//hitArray[i] = new RaycastHit( );
}
RaysArr = new Ray[cars.Length];
hitArray = new RaycastHit[cars.Length];
//i dont know how to declare raycasthit array
}
void update()
{
for(int i =0;i<cars.Length;i++)
{
RaysArr[i]= new Ray(cars[i].transform.position,cars[i].transform.forward);
if(Physics.Raycast(RaysArr[i],out hitarry[i] ,10f))
{
dosomething();//if hits
}
else
{
dosomething();//if doesnt hits
}
}
}
Please edit your question into a comprehensible state and watch this: http://video.unity3d.com/video/7720450/tutorials-using-unity-answers
The approach should work, but there are some problems in the code. What is the ultimate goal here. Do you really need to save the hit points, or is the "dosomething()" the ultimate goal? If you don't have to save the hit points, this code can be simplified a lot.
sorry for the late reply thanks for the support guys. i simplified it and now its fine.i could perform multiple ray cast now.thanks to forum members and thanks to unity.