x


Destroy and Spawn an Enemy

Hello,

I am working on a

  1. Side scroller ( meant that the view is 2D) - only on X-Y plane. Z always '0'.
  2. The canon has to be placed on the left most corner and fire in a Projectile Fashion

Game Objects :

I have placed a script called as "enemypos" on an empty gameobject. Then there is another prefab called as the "enemy" which is the enemy.

Code:


The enemypos script is as follows :

var projscript : projectile;
var ene_pos: Transform;
var pos : float;


function Awake()
{
    e_spawn();
}


function Update ()
{


}


function e_spawn()
{
    pos = projscript.spawn();
    var position: Vector3 = Vector3(pos, 1, 0);
    var enemy = Instantiate(ene_pos, position, Quaternion.identity);
}

The spawn() function is as follows: (is called from another script)

 function spawn()
    {
        if(Random.Range(0,10)<7)
           return Random.Range(-18,10);
        else
           return Random.Range(10,21);

    }

Code Explanation:

The function e_spawn() is used to instantiate the enemy prefab.

Request:

My humble request is how to destroy and instantiate the same enemy along the x axis.

THANK YOU in advance

more ▼

asked Dec 07 '11 at 07:41 AM

Karsnen gravatar image

Karsnen
736 26 45 54

Fellow Unity Users - My original question and it's explanation was wrong but now I have edited it.

Could someone kindly help me on this?

Dec 07 '11 at 05:48 PM Karsnen
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

If I understand well, in the function spawn() you randomize the point on which the new enemy will appears, and you wanna modify that script. If so:

 var enemy : GameObject;
 var y = 0 //this is a constant value, I think...

 function spawn()
    {
        if(Random.Range(0,10)<7)
           newX = Random.Range(-18,10);
        else
           newX = Random.Range(10,21);

        Instantiate(enemy, Vector3(newX,y,0), Quaternion.identity);
    }

I think that it could be a solution for your problem.

more ▼

answered Dec 07 '11 at 08:02 AM

BiG gravatar image

BiG
4.7k 4 13 49

Sorry mate - Really sorry I messed up things. I was so confused that I made something else on my Question. I should not have done that. I have changed the question right now. If it was not you, I do not think I could have noticed it. I hope I am clear now.

Dec 07 '11 at 08:24 AM Karsnen
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3444
x1251
x764
x652
x438

asked: Dec 07 '11 at 07:41 AM

Seen: 868 times

Last Updated: Dec 07 '11 at 05:48 PM