- Home /
[C#] Raycast and raycasthit, destroy and instantiate gameobjects
Hello, i'd like to have a raycast that, when hits something, destroys it and instantiate an explosion prefab. here's my code
Vector3 fwr = transform.TransformDirection (Vector3.forward);
if (IsReadyToShoot == true && WantsToShoot == true) {
WantsToShoot = false;
Laser.SetActive (true);
ASS.Play();
Invoke ("Unloader", 0.1f);
ReloadTime = 10f;
if ((Physics.Raycast (Ray0.transform.position, fwr, 20f, Enemies)) || (Physics.Raycast (Ray1.transform.position, fwr, 20f, Enemies)) || (Physics.Raycast (Ray2.transform.position, fwr, 20f, Enemies)) || (Physics.Raycast (Ray3.transform.position, fwr, 20f, Enemies)) || (Physics.Raycast (Ray4.transform.position, fwr, 20f, Enemies)) || (Physics.Raycast (Ray5.transform.position, fwr, 20f, Enemies))) {
Debug.Log ("Hit!");
}
as you can see i got 6 gameobject from which i cast 6 rays. i'd like to make a script that destroys every hit object that is hit from at least one ray, and to instantiata an explosion in the coordinates of the hit. i was trying to use raycasthit but even if i looked on the internet for long, i can't figure out how to make it work. thanks in advance
Answer by Habitablaba · Jun 23, 2016 at 05:07 PM
Use the version of Physics.Raycast that takes an "out RaycastHit hit" parameter. You'll also want to check each ray individually, so you'll be able to destroy more than just one object.
RaycastHit hit;
if (IsReadyToShoot && WantsToShoot ) {
WantsToShoot = false;
Laser.SetActive (true);
ASS.Play();
Invoke ("Unloader", 0.1f);
ReloadTime = 10f;
if ((Physics.Raycast (Ray0.transform.position, transform.forward, out hit, 20f, Enemies)) {
Destroy(hit.gameObject);
}
if ((Physics.Raycast (Ray1.transform.position, transform.forward, out hit, 20f, Enemies)) {
Destroy(hit.gameObject);
}
if ((Physics.Raycast (Ray2.transform.position, transform.forward, out hit, 20f, Enemies)) {
Destroy(hit.gameObject);
}
if ((Physics.Raycast (Ray3.transform.position, transform.forward, out hit, 20f, Enemies)) {
Destroy(hit.gameObject);
}
if ((Physics.Raycast (Ray4.transform.position, transform.forward, out hit, 20f, Enemies)) {
Destroy(hit.gameObject);
}
if ((Physics.Raycast (Ray5.transform.position, transform.forward, out hit, 20f, Enemies)) {
Destroy(hit.gameObject);
}
Also: You can use transform.forward to get an object's forward vector, instead of trying to transform the world forward vector. You don't need to check "== true" for boolean values, you can just check the value
Hopefully this is enough to get you started?
Thank you very much, you're an awesome person. Really clear and complete answer, good job. p.s. i noticed you're missing one parenthesis after each if p.p.s. Raycasthit does not support .gameObject directly you have to write .collider.gameObject to make it work
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
RayCast in WebGL not working 7 Answers
How do I move a raycast back on objects z axis? 1 Answer
Click On/Off Manager - C# 2 Answers