飞机大战

1.先做一个地面  让相机旋转的x轴变为90  
  让界面背景滚动,代码如下:
public class BG : MonoBehaviour {
    private Material material;

 void Start () {
        material = transform.GetComponent<Renderer>().material;
    }
 
 void Update () {
        material.mainTextureOffset = new Vector2(0,-Time.time*0.2f);
 }
}

2.加上飞机 让飞机移动,发射子弹, 且边界限定(还有一些关于文本的显示)
在坦克上面挂一个TankCtrl,和lua交互,代码如下:
public class TankCtrl : MonoBehaviour {
   
 // Use this for initialization
 void Awake () {
        Util.CallMethod("TankCtrl", "Awake", gameObject);
 }
 
 // Update is called once per frame
 void Update () {
        Util.CallMethod("TankCtrl", "Update", gameObject);
    }
}

lua中代码如下:

TankCtrl = {};
local this = TankCtrl;

local transform;
local gameObject;
--子弹位置
local firepos;
--子弹
local bullet;

function TankCtrl.Awake(go)
 gameObject = go;
 transform = go.transform;
 --找到子弹位置
 firepos=transform:Find("firepos");
 --加载子弹
    *****************预制体路径添加到Packager*****************
 bullet=resMgr:LoadPrefab('bullet', { 'bullet' }, this.InitBullet);
 --加载分数
 local s=transform:Find("Canvas/Text/score");
 this.score=s:GetComponent('Text');
 --加载倒计时
 local t=transform:Find("Canvas/Text (1)/time");
 this.times=t:GetComponent('Text'); 
 --加载生命
 local h=transform:Find("Canvas/Text (2)/hp");
 this.hp=h:GetComponent('Text');
end

--子弹的回调函数
function TankCtrl.InitBullet(gos)
 bullet=gos[0]
end

local num=60
function TankCtrl.Update()
    --倒计时显示
 num=num-0.02
 this.times.text=tostring(math.ceil(num))
 --移动
 local h=UnityEngine.Input.GetAxis("Horizontal");
 local v=UnityEngine.Input.GetAxis("Vertical");
 if (h~=0 or v~=0) then
  v3=transform.position
 --边界限定
 v3.x= Mathf.Clamp(v3.x+h*0.2,-4,4)
 v3.z= Mathf.Clamp(v3.z+v*0.2,-5,7)
 transform.position=v3
 end
 
 --生成子弹
 if UnityEngine.Input.GetMouseButtonDown(0) then
  temp=newObject(bullet)
  temp.transform.position=firepos.position
 end
end

3.敌方随机生成

在地面挂一个脚本enemyLoad,代码如下:

public class enemyLoad : MonoBehaviour {

 // Use this for initialization
 void Awake () {
        Util.CallMethod("enemyLoad","Awake",gameObject);
 }
 
 // Update is called once per frame
 void Update () {
        Util.CallMethod("enemyLoad", "Update", gameObject);
    }
}


lua中代码如下:


enemyLoad = {};
local this = enemyLoad;

local transform;
local gameObject;
local enemy;
local y=0

function enemyLoad.Awake(go)
 gameObject = go;
 transform = go.transform;
 加载敌方
 *****************预制体路径添加到Packager*****************
    enemy=resMgr:LoadPrefab('enemy', { 'enemy' }, this.InitEnemy);
end

function enemyLoad.InitEnemy(gos)
 enemy=gos[0]
end
   随机生成敌方的代码
function enemyLoad.Update()
 y=y+1
 if y%40==0 then
  temp= newObject(enemy)
  temp.transform.position=Vector3.New(math.random(-3.79,3.69),0.09,8.33)
 end
end
敌方上挂个脚本enemyPlane(敌方飞机)

using LuaFramework;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class enemyPlane : MonoBehaviour {

 void Update () {
        Util.CallMethod("enemyCtrl","Update",gameObject);
 }

    private void OnTriggerEnter(Collider other)
    {
        Util.CallMethod("BulletCtrl", "OnTriggerEnter", new object[] { this.gameObject,other.gameObject});
    }
}


敌方移动的代码
enemyCtrl = {};
local this = enemyCtrl;

function enemyCtrl:New(o,go)
 o=o or {}
 setmetatable(o,self)
 self.__index=self
 o.gameObject=go
 o.transform=o.gameObject.transform
 return o
end

function enemyCtrl:Update()
 self.transform:Translate(-self.transform.forward*UnityEngine.Time.deltaTime*10)
end

4.让子弹移动 ,还有和敌方的碰撞(得分和生面的文本显示)
先在子弹上加一个BulletCtrl,和lua交互,代码如下
public class BulletCtrl : MonoBehaviour {
 void Update () {
        Util.CallMethod("BulletCtrl", "Update", gameObject);
 }
}

lua中代码如下:

BulletCtrl = {};
local this = BulletCtrl;

function BulletCtrl:New(o,go)
 o=o or {}
 setmetatable(o,self)
 self.__index=self
 o.gameObject=go
 o.transform=o.gameObject.transform
 return o
end

function BulletCtrl:Update()
 self.transform:Translate(self.transform.forward*UnityEngine.Time.deltaTime*30)
end
local num=0;
local num1=5;
--碰撞检测
function BulletCtrl.OnTriggerEnter(...)
 local res={...}
 local enemy=res[1]
 local bullet=res[2]
 
 if bullet.name~="plane" then
  destroy(enemy)
  destroy(bullet)
  else
  当敌方和自己的飞机碰撞,生命-1
  num1=num1-1
  TankCtrl.hp.text=tostring(num1)
  当生命=0,自己的飞机销毁
  if num1==0 then
   destroy(bullet)
  end
  destroy(enemy)

 end
  碰撞成功后,得分加1
 num=num+1
 TankCtrl.score.text=tostring(num)
end

备注: 碰撞一些细节
    自己飞机 刚体 和碰撞器(isTrigger点上)
    敌方     碰撞器(isTrigger不点)
 子弹     刚体 和碰撞器(isTrigger点上)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值