游戏场景里 坦克移动 发射子弹 打坦克
给坦克加角色控制器,加个TankCtrl 脚本进行和lua的交互
using LuaFramework;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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);
}
}
void Awake () {
Util.CallMethod("TankCtrl", "Awake", gameObject);
}
// Update is called once per frame
void Update () {
Util.CallMethod("TankCtrl", "Update", gameObject);
}
}
lua创建一个TankCtrl脚本
TankCtrl = {};
local this = TankCtrl;
local this = TankCtrl;
local transform;
local gameObject;
--角色控制器
local cc;
--子弹位置
local firepos;
--子弹
local bullet;
--所有的子弹
local bullets={};
local gameObject;
--角色控制器
local cc;
--子弹位置
local firepos;
--子弹
local bullet;
--所有的子弹
local bullets={};
function TankCtrl.Awake(go)
gameObject = go;
transform = go.transform;
--找到角色控制器
cc=gameObject:GetComponent("CharacterController");
--找到子弹的位置
firepos=transform:Find("firepos")
--加载预制体
bullet=resMgr:LoadPrefab("Bullet",{"Bullet"},this.loadprefab)
end
--回調函數
function TankCtrl.loadprefab(gos)
bullet=gos[0]
end
function TankCtrl.loadprefab(gos)
bullet=gos[0]
end
function TankCtrl.Update()
--按WS移动
if UnityEngine.Input.GetKey(UnityEngine.KeyCode.W) then
cc:SimpleMove(transform.up*UnityEngine.Time.deltaTime*500)
elseif UnityEngine.Input.GetKey(UnityEngine.KeyCode.S) then
cc:SimpleMove(-transform.up*UnityEngine.Time.deltaTime*500)
end
--按AD旋转
if UnityEngine.Input.GetKey(UnityEngine.KeyCode.A) then
transform:Rotate(Vector3.back*2)
elseif UnityEngine.Input.GetKey(UnityEngine.KeyCode.D) then
transform:Rotate(Vector3.forward*2)
end
--点击鼠标左键生成子弹
if UnityEngine.Input.GetMouseButtonDown(0) then
--实例化子弹
temp= newObject(bullet)
--设置子弹位置
temp.transform.position=firepos.transform.position
--设置子弹方向
temp.transform.rotation = firepos.rotation
--创建子弹控制器
bc=BulletCtrl:New(nil,temp);
--添加到表里
table.insert(bullets,bc);
--遍历所有的子弹,让所有子弹都执行Update
if table.getn(bullets)>0 then
for i,v in ipairs(bullets) do
v:Update();
end
end
end
end
子弹预制体加个BulletCtrl 和lua交互
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LuaFramework;
using System.Collections.Generic;
using UnityEngine;
using LuaFramework;
public class BulletCtrl : MonoBehaviour {
void Awake()
{
Util.CallMethod("BulletCtrl", "Awake", gameObject);
}
{
Util.CallMethod("BulletCtrl", "Awake", gameObject);
}
// Update is called once per frame
void Update()
{
Util.CallMethod("BulletCtrl", "Update", gameObject);
}
}
void Update()
{
Util.CallMethod("BulletCtrl", "Update", gameObject);
}
}
lua中 写个BulletCtrl
BulletCtrl={}
local this=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
function BulletCtrl:Update()
self.transform:Translate(self.transform.forward * UnityEngine.Time.deltaTime * 30)
end
--子弹销毁
function BulletCtrl.Attack(...)
local res={...}
local bullet=res[1];
local emerytank=res[2];
destroy(bullet)
destroy(emerytank)
end
function BulletCtrl.Attack(...)
local res={...}
local bullet=res[1];
local emerytank=res[2];
destroy(bullet)
destroy(emerytank)
end
敌方坦克加个emeryTankCtrl
using LuaFramework;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class emeryTankCtrl : MonoBehaviour {
private void OnTriggerEnter(Collider other)
{
Util.CallMethod("BulletCtrl","Attack",new object[] { this.gameObject,other.gameObject});
}
}
{
Util.CallMethod("BulletCtrl","Attack",new object[] { this.gameObject,other.gameObject});
}
}