x


Instantiating a Prefab based on Input(Javascript)

Hi I am making a 2d side scroller where my ship has 3 forms. The forms are changed using Input.GetKeyDown. When my player's ship enters for instance Fire form, I would like a Particle System to envelop the players ship with a red colored aura. My problem is since I have 3 elemental auras and one ultimate aura I need to be able to tell Unity which Aura Prefab to attach to the player at the moment of input. Also how do you "stick" it to the ship so after it has changed forms the aura stays around the ship while its moving, instead of just staying at the point where it was "called"

Alot of this script isnt finished so some of the statements dont do anything yet. Thanks for the help.

var playerSpeed : int;
var playerLives : int;
var playerHealth : int;
var playerAura : Transform;

static var isEarthForm : boolean = false;
static var isFireForm : boolean = false;
static var isWaterForm : boolean = false;
static var isUltimateForm : boolean = false;


static var playerFormPoints : int;
static var playerScore : int;
static var bulletPower : int = 1;


var bullet : Rigidbody;
var explosion : Transform;



function Update () {
    //how much to move the player
    amtToMoveX = (playerSpeed * Input.GetAxis("Horizontal")) * Time.deltaTime;
    amtToMoveY = (playerSpeed * Input.GetAxis("Vertical")) * Time.deltaTime;

    //translate player
    transform.Translate(Vector3.right * amtToMoveX);
    transform.Translate(Vector3.up * amtToMoveY);


    //looks to see if left mouse button is prssed, if it is, fires a bullet
    if(Input.GetKeyDown("mouse 0")){
       var tempBullet : Rigidbody;

       tempBullet = Instantiate(bullet, transform.position, transform.rotation);
    }

    //activates Ultimate Form
    if(Input.GetKeyDown("q" || "v")){
       if(playerFormPoints > 2){

         isUltimateForm = true;
         bulletPower = 3;
         print("Ultimate Form Achieved");
       }
    }

    //activates Fire Form
    if(Input.GetKeyDown("f" || "c")){
       isFireForm = true;
       print("Fire Form Achieved");
    }

    //activates Water Form
    if(Input.GetKeyDown("r" || "x")){
       isWaterForm = true;
       print("Water Form Achieved");
    }

    //activates Earth Form
    if(Input.GetKeyDown("e" || "z")){
       isEarthForm = true;
       print("Earth Form Achieved");
    }

}//end update

function OnGUI(){
    GUI.Label(Rect(10,10,200,50), "Score: " + playerScore);

    GUI.Label(Rect(10,30,200,50), "Lives: " + playerLives);

    GUI.Label(Rect(10,50,200,50), "Health: " + playerHealth);

    GUI.Label(Rect(10,70,200,50), "Perfect Form: " + playerFormPoints);
}

function OnTriggerEnter(otherObject : Collider){
    if(otherObject.gameObject.tag == "enemy" || otherObject.gameObject.tag == "enemyBullet"){
       playerHealth--;

       //respawns enemy
       otherObject.gameObject.transform.position.x = 30;
       otherObject.gameObject.transform.position.y = Random.Range(-13,11);


       var tempExplosion : Transform;

       tempExplosion = Instantiate(explosion, transform.position, transform.rotation);
    }

}
more ▼

asked Mar 28 '12 at 06:28 AM

kiljosh gravatar image

kiljosh
0 1 2

You need to go on the reference script for Mouse input . The proper way to use it is Input.GetMouseButtonDown(0); also, you cannot (x || y) you need to write the whole command for each key. At least I have never seen it before.

Mar 28 '12 at 06:51 AM fafase
(comments are locked)
10|3000 characters needed characters left

2 answers: sort oldest

As it is sometimes usefull to instantiate a prefab on input ( => Instantiate(prefab, pos, Quaternion.identity).transform.parent = transform.parent), this isn't the case here.

You should have all the particle systems you need as children of your ship. If it's Shuriken, make sure they don't pley on awake, if it's legacy they must have emit disabled. Then Play / Emit the one you need when you need it.

About sticking part, try changing the space of the particle system.

more ▼

answered Mar 28 '12 at 07:20 AM

Berenger gravatar image

Berenger
11k 12 19 53

(comments are locked)
10|3000 characters needed characters left

Thanks for your input guys, I understand the concept your trying to convey I just don't really understand how to execute it properly. I made the changes to my code suggested, I parent'ed my player's ship to the auras, turned the aura's emit off, and cleaned up the boolean code like suggested but I'm getting an error saying

"MissingFieldException: Field 'CompilerGenerated.__playerScriptUpdate$callable1$9336.particleEmitters' not found."

I know it's a simple solution from here but I am only a month into learning Unity/Jscript coming from Java.

this playerScript is attached to my player game object, in the inspector I have added the auras into the element fields in the correct order.

Im thinking my problem is how I'm initializing the array. Also having trouble getting to the emit in each of the children and setting it to true. I have attached the relevant code... thanks for your help.

var particleEmitters : ParticleEmitter[]; 

function Awake () {

    particleEmitters = gameObject.GetComponentsInChildren(ParticleEmitter) as ParticleEmitter[];

}

function Update () {
//looks to see if left mouse button is prssed, if it is, fires a bullet
    if(Input.GetMouseButtonDown(0)){
       var tempBullet : Rigidbody;

       tempBullet = Instantiate(bullet, transform.position, transform.rotation);
    }

    //activates Ultimate Form
    if(iKD("q") || iKD("v")){
       //checks to see if form is currently active, if it is it turns it off and reloads Update                      ////////////////ALL 4 FORMS STILL NEED TO SHUT OFF AURAS DEPENDING ON
       if(isUltimateForm == true){                                                       ////////////////INPUT   WHEN A KEY FOR AURA IS PRESSED BUT ITS ALREADY ACTIVE
         isUltimateForm = false;  
         Update();
       }


       if(playerFormPoints > 2){
         gameObject.GetComponentInChildren.particleEmitters[3].emit = true;
         isUltimateForm = true;
         bulletPower = 3;
         print("Ultimate Form Achieved");
       }


    }

    //activates Fire Form
    if(iKD("f") || iKD("c")){
       //checks to see if form is currently active, if it is it turns it off and reloads Update
       if(isFireForm == true){
         isFireForm = false;  
         Update();
       }


       gameObject.GetComponentInChildren.particleEmitters[0].emit = true;
       isFireForm = true;
       print("Fire Form Achieved");
    }

    //activates Water Form
    if(iKD("r") || iKD("x")){
       //checks to see if form is currently active, if it is it turns it off and reloads Update
       if(isWaterForm == true){
         isWaterForm = false; 
         Update();
       }



       gameObject.GetComponentInChildren.particleEmitters[1].emit = true;
       isWaterForm = true;
       print("Water Form Achieved");
    }

    //activates Earth Form
    if(iKD("e") || iKD("z")){
       //checks to see if form is currently active, if it is it turns it off and reloads Update
       if(isEarthForm == true){
         isEarthForm = false; 
         Update();
       }


       gameObject.GetComponentInChildren.particleEmitters[2].emit = true;
       isEarthForm = true;
       print("Earth Form Achieved");
    }

}//end update
more ▼

answered Mar 28 '12 at 08:48 PM

kiljosh gravatar image

kiljosh
0 1 2

Instead of calling GetComponentInChildren.particleEmitters[2]..... you just call the array of which these components populate. So, just call

particleEmitters[SomeNumber].emit=true;

Since the array is containing these components already.

Im not sure you want(or can call) Update(), it gets called all the time anyway. Id remove that.

Also, i dont see the var for iKD. just add it at the top, unless you excluded it for this post.

Mar 29 '12 at 04:15 AM hijinxbassist
(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:

x1680
x1259
x5

asked: Mar 28 '12 at 06:28 AM

Seen: 418 times

Last Updated: Mar 29 '12 at 04:16 AM