Hi there! I'm a total newbie regarding unity engine. I've done some small tutorials. Now im trying to create my own "Alien Invaders" clone. I think i got most of the stuff i need to do regarding my game but I've got stuck with some pretty stupid thing. I've got 3 game objects. Bullet, BlockEraser and BaseBlock. When I hit BaseBlock with Bullet i want to spawn BlockEraser. This way eventually my BaseBlock gets destroyed and Bullets should fly through BlockEraser but should get destroyed on BaseBlock collision. Image with black background is what i want to achieve. What i did currently is that I added rigid body to Bullet, box colliders for all of the 3 game objects and checked Bullet isTrigger. I attached the script for Bullet object and added this code in it: private GameObject eraser; void Start() { eraser = GameObject.FindGameObjectWithTag("EraserBlock"); } void Update() { Vector3 temp = gameObject.transform.position; temp.x += 1f*Time.deltaTime; gameObject.transform.position = temp; } private void OnTriggerEnter2D(Collider2D hitobject) { if (hitobject.tag == "BaseShield") { if (hitobject.tag != "EraserBlock") { CreateEraserBlock(gameObject); } } } void CreateEraserBlock(GameObject gameObject) { Instantiate(eraser, gameObject.transform.position, Quaternion.identity); Destroy(gameObject); } My problem arises when the Bullet hits the BaseBlock. Everytime it spawns EraserBlock at the same location (image with blue background provided just to showcase the problem), instead it should progressively create a hole(tunnel) through BaseBlock. Any help would be appreciated. Thnx
use code tags for better readable. And I am not sure, that the script will run from the first try Code (CSharp): private GameObject eraser; //I think here you should use prefab from the eraser //public GameObject eraser; //here you can change the speed from the bullet //public float speed; void Start() { eraser = GameObject.FindGameObjectWithTag("EraserBlock"); //as said, maybe prefab, then no need to look for it } void Update() { Vector3 temp = gameObject.transform.position; //adjust the speed from the editor //temp.x += speed * Time.deltaTime; temp.x += 1f * Time.deltaTime; gameObject.transform.position = temp; } private void OnTriggerEnter2D(Collider2D hitobject) { if (hitobject.tag == "BaseShield") { //I see no use in this second check, the tag here is already other as EraserBlock (== Baseshield) if (hitobject.tag != "EraserBlock") { //CreateEraserBlock(gameObject); //and I think you will destroy the green block and the bullet? CreateEraserBlock(hitobject.gameObject); } } } //void CreateEraserBlock(GameObject gameObject) //use here an other word as parameter, gameObject is by default this gameobject void CreateEraserBlock(GameObject hittedObject) { //delete green block Destroy(hittedObject); //spawn the eraser on the place from the destroyed block Instantiate(eraser, hittedObject.transform.position, Quaternion.identity); //was this object bullet ? here you can delete it Destroy(gameObject); }
Thnx for trying to help me.. Problem is not destroying the whole green object(base).. I want to destroy only part of it in the way i spawn black object over the green one.. That way i want to destroy it piece by piece. I dont know how to achieve my intention. Thnx
I thought, the base was builded from the many small green blocks Then this would be working like here: you can try to make the base without collider. Place the erasers over the base with enabled collider but disabled sprite renderer. Then, if the collider was hitted, disable collider and enable sprite renderer. other way, I think, you should change the texture and poligon collider from the base. Sorry, I have never did it. You can try to google: destructible 2d terrain like worms.