射击游戏

-------------Player的代码------------------------

var bullet:GameObject; //子弹
var Dummy:GameObject;//虚拟体
var playerSpeed:float;//左右移动操作速度
static var fire=false; //是否开火
function Update ()
{
   //操作代码
   var antmove:float = Input.GetAxisRaw("Horizontal")*playerSpeed*Time.deltaTime;
   transform.Translate(Vector3.right*antmove);
   //发射代码
   if (Input.GetKeyDown("space"))
   {
   Instantiate(bullet, Dummy.transform.position, Dummy.transform.rotation);
   fire = true;
   }
}

-----------------子弹运动和毁灭----------------------------

var bulletSpeed:float;
function Update ()
{
  if(player.fire == true) //PLAYER是上面的脚本名
  {
  transform.Translate(0,Time.deltaTime*bulletSpeed,0);
  if (transform.position.y >5)
  {
    Destroy(gameObject);
  }
  }
}

----------------------------一个根据碰撞次数毁灭并产生粒子------------------------------

//计算碰撞次数
private var NumberForCollsion=0; //用于保存碰撞次数的变量
private var DisplayNumberForCollsion="";//用于在屏幕上显示碰撞次数
var Clone:Transform;//添加粒子

function OnCollisionEnter(collisionInfo : Collision) {
 NumberForCollsion+=1;
 DisplayNumberForCollsion="碰撞次数:";
 if (NumberForCollsion == 1)
 Destroy(gameObject);  
 DisplayNumberForCollsion = DisplayNumberForCollsion+NumberForCollsion;
 Clone = Instantiate(Clone, transform.position, transform.rotation);
}

function OnGUI () {
GUI.Label (Rect (10, 10, 100, 20), DisplayNumberForCollsion);
}

你可能感兴趣的:(游戏)