- Home /
Raycast Specific Object/Instantiate Explosion
Hello Again!
In my current(Unity 3.3 IOS)project I have a character walking across a bridge. If the character falls off the bridge he is to fall into a fiery river and explode. The problem is when he's on the bridge the Raycast reads the rigidbody on the bridge and he immediately explodes. If I reposition him in the scene window to an open air position, the gravity causes him to fall into the river and explode as planned. I added the following line of code to designate only destroy if the raycast hits the "plane". It doesn't work.
if(hit.collider.gameObject.name == "plane");
The character does not get destroyed on the bridge or when he hits the plane/fiery river. There are several bridges and buildings he will be walking into, so I only want him to be destroyed/explode if the raycast hits the plane. Can anyone tell me why my code isn't working or how to correct it? Here is my complete Raycast code.
var explosion: Transform;
var point :Vector3;
var explosionRotation :Quaternion;
function Update(){
var hit :RaycastHit;
var dwn = transform.TransformDirection(Vector3.down);
if (Physics.Raycast(this.transform.position,dwn,hit,3))
if(hit.collider.gameObject.name == "plane")
{
point = hit.point;
explosionRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
Explode();
}
}
function Explode()
{
Destroy(this.gameObject);
var instanExplosion = Instantiate(explosion, point, explosionRotation);
}
Thanks for any advice and assistance in advance.
Respectfully,
Digital D
the code looks fine, are you sure your test object name is correct? it's "plane" and not "Plane"? string comparison is case sensitive
you could probably benefit from using gameObject tags though ins$$anonymous$$d of checking the game objects name
Thanks pyro! You were correct. In the Hierarchy window the name was "Plane". It works as designed now.
Answer by flaviusxvii · May 23, 2011 at 07:09 PM
Why are you using a raycast? Use colliders and events for something like this.
I used raycast because I want the explosion to happen just above the fiery lake. With raycast I can control the distance from the event, ins$$anonymous$$d of when the objects collide. Just a little detail thing I guess.
flavi is correct. you are basically doing it wrong: just use colliders, triggers, and events. Raycasts are irrelevant here! Hope it helps someone reading.
Your answer
Follow this Question
Related Questions
how to raycast instantiated game characters 1 Answer
Destroying object using his name and raycast 2 Answers
How to delete an instantiated object 0 Answers
Object won't instantiate at parents position(solved) 2 Answers
Destroy the current GameObject? 7 Answers