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?
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.
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?