sabato 7 settembre 2013

Worms bar shoot example > Unity3d

Just one of my quick exercise I did when I started to play with Unity. Here we see the code that produces the bullet given a certain direction, you can increase the power of the shoot by holding space.

var gui_grandezza : int = 256;
var potenza : float = 0;
private var increasing : boolean = false;
var shooting : boolean = false;
var barraspeed : int = 100;
var proiettile : GameObject;
var spawnpoint : Transform;
var shotforce : float = 5;
var cubi : GameObject;
private var currentCrates : GameObject;
var blastPart : ParticleEmitter;
var altezza : GUITexture;

function Start(){
 guiTexture.pixelInset.width = 0;
 var somecrates : GameObject = Instantiate(cubi, Vector3(8,1,-6), transform.rotation);
 currentCrates = somecrates;
}

function Update(){
 if(!shooting && Input.GetButtonDown("Jump")){
  increasing = true;
 }
 var vertical : float = Input.GetAxis("Vertical") * -1;
 spawnpoint.Rotate(Vector3(vertical,0,0) * Time.deltaTime * 10);
 altezza.guiTexture.pixelInset.y += vertical * -1;
 
 if(!shooting && Input.GetButtonUp("Jump")){
  increasing = false;
  shoot(potenza);
  potenza = 0;
 }
 
 if(increasing){
  potenza += Time.deltaTime * barraspeed;
  potenza = Mathf.Clamp(potenza, 0, gui_grandezza);
  guiTexture.pixelInset.width = potenza;
 }
 
}

function shoot(potenza : float){
 shooting = true;
 var pBlast : ParticleEmitter = Instantiate(blastPart, spawnpoint.position, spawnpoint.rotation); //just a particle emitter
 //base blast amount on power argument, and divide it to diminish power
 pBlast.maxEmission = potenza/10;
 var pFab : GameObject = Instantiate(proiettile, spawnpoint.position, spawnpoint.rotation); //that's our bullet
 pFab.rigidbody.AddForce(spawnpoint.forward * potenza * shotforce);

//we reset the envoirment after 4 seconds
 Destroy(pFab.gameObject, 4);
 Destroy(pBlast.gameObject, 1);
 yield WaitForSeconds(4);
 potenza = 0;
 guiTexture.pixelInset.width = 0;
 Destroy(currentCrates);
 shooting = false;
 var somecrates2 : GameObject = Instantiate(cubi, Vector3(8,1,-6), transform.rotation);
 currentCrates = somecrates2;
}


When we hold the "jump" button (space by default) the variable increasing is set to true. When increasing is true we use potenza += Time.deltaTime * barraspeed; potenza = Mathf.Clamp(potenza, 0, gui_grandezza); to add power to our shoot. When the jump button is released Input.GetButtonUp("Jump") returns true, at this point we call the function shoot(potenza) and we set increasing to false. The function shoot instantiate a particle emitter and our bullet. The bullet pFab has a rigidbody, we use AddForce to make it go forward from our swappoint with a velocity of potenza * shotforce. Most of the variables names are in italian, I hope it's not that difficult to follow, in the end it's a really simple example, here you can download the full project.
-------------------
ITA: Il codice cerca di simulare la barra dello sparo di Worms, tenendo premuto incrementiamo la variabile potenza, al rilascio del tasto spazio usiamo la funzione shoot per instanziare un rigidbody al quale aggiungeremo una forza tramite la funzione AddForce. La stessa funzione shoot si occupa di resettare la scena, l'intero esempio è scaricabile dal seguente link.

Nessun commento:

Posta un commento