- Home /
Question by
sdgd · Mar 18, 2013 at 12:33 AM ·
c#raydestroy objectraycastall
how to destroy all GameObjects ray went through
what I'm trying to do is to destroy all objects ray went through them in a range of 6m and not just 1 object (first).
I know I should use RaycastHit[]
but this gives basic idea what I'm trying to achieve
and OFC this is not on Update function so it's only 1 go to find them all.
RaycastHit RayHits;
if (hit[0]){
ray = new Ray ((new Vector3(transform.position.x, transform.position.y+2, transform.position.z)),
transform.TransformDirection(Vector3.forward));
Debug.DrawRay ((new Vector3(transform.position.x, transform.position.y+2, transform.position.z)),
transform.TransformDirection(Vector3.forward*6), Color.red, 5f);
if (Physics.RaycastAll(ray, out RayHits, 6f)){
Destroy(RayHits.collider.gameObject);
}
}
Comment
Best Answer
Answer by robertbu · Mar 18, 2013 at 01:09 AM
Something like this:
Ray ray = new Ray ((new Vector3(transform.position.x, transform.position.y+2, transform.position.z)),
transform.TransformDirection(Vector3.forward));
Debug.DrawRay ((new Vector3(transform.position.x, transform.position.y+2, transform.position.z)),
transform.TransformDirection(Vector3.forward*6), Color.red, 5f);
RaycastHit[] hits = Physics.RaycastAll(ray, 6f);
foreach (RaycastHit hit in hits) {
Destroy(hit.collider.gameObject);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
ray transform.position goes wrong 1 Answer
Dumb this down for me? 1 Answer
Raytrace Ignore the x axis ? Or Am I just a noob ? x') 0 Answers