- Home /
use list of gameobjects transform in raycast
Hey i would like to cast a raycast that involves more than 1 raycasthit. I have a list of gameobjects that i want to use. I want to use these gameobjects and their transform to position where i want to shoot these raycasts.
public void GetParts()
{
aquiredParts = aquiredShip.GetComponent<Ship>().shipParts;
}
aquiredParts is the list with the gameObjects i wanna raycast on. I access this function from another script which is on a parent gameobject. The list consists of the childs of this parent and it updates everytime i aquire a new gameobject. This is how it looks in that script:
public void CheckParts()
{
foreach (Transform child in gameObject.transform)
{
if (child.gameObject.tag == "shipPart")
{
shipParts.Add(child.gameObject);
}
}
}
Since both these functions run in start() the list will always update with each new created object.
But for the main point of the question. How can i cast several raycasts (either a raycast array or a raycastAll) on each of these gameobjects that are in this list?
Thanks.
foreach(GameObject go in shipParts) {
RaycastHit hit = new RaycastHit();
if(Physics.Raycast(go.transform.position, go.transform.forward, out hit) {
}
}
Just to expand on this slightly, this RaycastHit "hit" passed OUT from Physics.Raycast will include the First collider hit by the raycast operation. Note: Chero's suggest will raycast "forward" from each ship part- I'm not sure that's actually what you want it do. For example, if detecting a hit by an enemy laser beam, your Ray's source and direction would remain constant for all those Raycasts.
You could also use RayCastAll, which returns an array of RaycastHit objects, that represents ALL the colliders hit by the ray. You can then go through that list to find "shipPart" objects, or whatverer.
Yeah the direction of the raycast can change since i can rotate the object with another function. However i need each raycast to meet the same condition if the if-statement is gonna go through. (for example: if hit.collider.tag == "placeToBuild"). All the raycasthits need to get gameobject "placeToBuild for the if statement to be true, not that only one match is needed to break the loop.
Ah, ok. And do you need to detect if the raycast hits another object before it reaches your target object? Or do you want to ignore all other objects?
If you want to ignore the other objects use collider.raycast. (http://docs.unity3d.com/ScriptReference/Collider.Raycast.html)
If you need to detect other objects that might be in the way, use physics.raycast (http://docs.unity3d.com/ScriptReference/Physics.Raycast.html)
Either way it sounds like you will need to make the raycast function call a FEW times, once for each ray, and logically AND all the results of your hit-tag-checking logic.
Your answer
Follow this Question
Related Questions
Keep adding targets to a list 2 Answers
how to get the snake body parts to follow the snake head? 1 Answer
Layered Collisions 1 Answer
How Should I Get a List of Child Objects 2 Answers