- Home /
[SOLVE] 2D Space shooter - explosion power-up scripting, please help!
I'm making a 2D space shooter and want to have a power-up system.
Edit : I know my script is wrong, I want the player to trigger the power-up, and the power-up only affects the gameObjects tag with Enemy, as I want to delete everything that is collides/inside the explosion prefab when it triggers, I also want this to occur for at least 5 seconds. I hope this edit clear things up
I want that if my player collides with the gameObject, it will create an explosion that will destroy all the enemies that enters it and all that was already inside it.
I have this right now
using UnityEngine;
using System.Collections;
public class FusionPowerUp : MonoBehaviour
{
public GameObject fusPrefab;
void OnTriggerEnter(Collider otherObject)
{
if(otherObject.tag == "Player")
{
var expPrefab = Instantiate(fusPrefab, this.transform.position, Quaternion.identity);
//Destroy(gameObject.tag = "Enemy"); // not working
Destroy(expPrefab, 2f);
Destroy(this.gameObject);
}
}
}
So right now when my player hits the object, it shows the explosion prefab, but thats all I've got. Also how do I allow my player to go pass this object, as right now the expPrefab collider doesn't allow my player to pass through it.
Also about scoring, how do I check when an enemy dies from the explosion and add it to my score? Right now I can add score when I shoot something with my bullet collider, but with this power-up it could destroy more than one enemy at a time.
Please help!
Thank you very much
One question at a time dude :) Well, firstly, make sure you checked the box isTrigger to make the collider a trigger area.
Before destroying the trigger area, you should destroy all enemies. In your code, you say if otherObject.tag is Player, but if it is player how can it be also Enemy? You need to change the structure. If you explain one by one, people here will help you better and faster.
Oh I really don't know how to do that, I want the player to trigger the power-up object but I want the effects to only affect the enemy.
So how would I script that? Please help
Answer by Tanshaydar · Oct 03, 2013 at 11:26 AM
This is just brainstorming. Have a Sphere collider for power up object. Check it isTrigger on inspector screen and when the player enters, have a boolean playerEntered that becomes true.
Then you can destroy power up object in, let's say, 2 seconds. When that boolean is true you can do something like this:
void OnTriggerEnter(Collider otherObject)
{
if(otherObject.tag == "Player")
{
playerEntered = true;
var expPrefab = Instantiate(fusPrefab, this.transform.position, Quaternion.identity);
Destroy(expPrefab, 2f);
Destroy(this.gameObject, 1.2f);
}
}
void OnTriggerStay(Collider otherObject)
{
if(otherObject.tag == "Enemy" && playerEntered)
{
Destroy( otherObject);
}
}
I will try this now, give me 5 $$anonymous$$utes, sorry I missed this answer.
Ok It doesn't delete the enemies, both enemies and the player just passes through the expPrefab.
Both my enemies and expPrefab have a collider and isTriggered.
This script was not a "finalized working script", it is just an idea about how to achieve your goal. Also, you should place this script on prefab object, not on the player or the enemy. $$anonymous$$ake sure your player is tagged as Player and enemies are tagged as Enemy.
Sorry the script is working like what I had before, it just doesn't delete the enemies. $$anonymous$$y player can successfully collide with the power-up box object, and it explodes. The problem is that the explosion doesn't delete any enemies.
I have all the tags correct, both have sphere collider and isTriggered on.
Which sphere collider you are using to detect enemies? Explosion prefab's? Player's? Enemies'? Power up object's?