- Home /
How to shoot prefab, and then on colliding destroy a gameobject?
Hey! I'm making a battleship game and so far, it been good but now i want to add a script that when i'm shooting my prefab(my bullet), in colliding with a game object(rival boat) an animation plays, and then destroy.Gameobject. i'm not some scripting guru, and now i'm stuk! can anyone help me with this?
Answer by Hybris · Jun 23, 2012 at 01:24 PM
Well, I will give you some tips to start. To begin it's not
destroy.Gameobject
it's
Destroy(GameObject);
You already know how to shoot right? If not you can use the shoot function I gave you in the question of the turret AI(yes that was me)
Well to check if the bullet hit something do this:
function OnCollisionEnter(hit : Collision){
if(hit.transform.tag == "rivalboat"){//this checks if the object you hit is a
//transform tagged "rivalboat"
yield WaitForSeconds(anim.length);
Destroy(hit.GameObject);
}
}
This script would go to the bullet. you need to define anim as an animation for this to work. cuz I think you meant that it plays an animation if it's dead or hit and then it destroys.
Little misunderstanding, If you want to use the explosion prefab, just instantiate it:
var explosion : Rigidbody;//Need to apply a rigidbody 2 the prefab if you use this
var explosionSpawn : Transform
and then:
Instantiate(explosion, explosionSpawn.position, explosionSpawn.rotation);
Good Luck -Hybris
PS: Dont forget to give an upvote or mark it as right answer(the green button beneath the downvote)so it gets removed from the unsolved list and the users who answered it don't get their karma.
noted! :) And I was still waiting for an anwser of you in that matter, when ever u want, just need to pass here: http://answers.unity3d.com/questions/256809/turret-ai-question.html about this one, i still dnt have my animation on it, i wanted to place it in the middle of this script, u thinks it is too complicated? I already have my prefab of the explosion(the animation, i will play), just wanted to open a new var animation : transform; so i can change it whenever i want, but i dnt know how to "play" this animation..
I already answered that one, the one you asked with the screenshot right? Did that, and I updated the code, and threw a comment in! if you still have a problem contact me(I check my recent answers alot)! and about the animation, I will edit my answer!! quick tip
A transform is a gameobject in a scene with a vector3(x, y, z), the vector3 is the position in the scene. A animation is not an object so it's impossible to assign an animation to a transform, animation are complicated I'm gonna update my answer now!
Haha, I even found an error in my own script
If you have any questions, or need anything contact me at: thomasoosterbeek@hotmail.com
But my animation is in a prefab! from the explosion framework of unity assets, i was planing on using one of those, can't it be adapted to that?
Oh, like the explosion prefab! That's not an animation! Those are particles! I will edit again! I will keep the part about animations for others!
ok! just instaling messenger, to speak to u, i prefer skype :P anyways, so now u have created two variables, i suspect that the first one should be for my explosion prefab with an rigid body attached to him, but wath about the second one?
Answer by generic cereal · Feb 15, 2013 at 01:02 AM
Do you think you could post what this entire script would look like? Sorry I'm very new to scripting... are there any vars? How do you know if your GameObject is a transform?
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Scripting BUG: target becomes bigger when getting nearer 1 Answer
Can the animation editor create local rotational data? 3 Answers
fireball animation 2 Answers
Is it possible to manipulate Mecanim state machine / blend trees from scripting? 0 Answers