Levitating Flying Code [In-Game Scripting]

#1
This simple code will turn every block of any object into an antigravity propulsion generator, allow it to soar through the air with ease! This initial script doesn't have impact compensation, so you have to correct orientation with primitive airfoil-based devices. Future versions may fix this, but for now it's still pretty darn cool.
Controls are

  • right/left: right/left
  • up/down: z/c
  • forward/back: up/down
but they can easily be changed in the code. If you aren't sure what to call a key, use this list.

Be warned: this is fairly laggy! I wouldn't recommend trying it on even medium-sized builds unless you have a really kick-ass computer.

Code:
import("UnityEngine")
blocklist={};infolist={};bodlist={}
dragweird={['FLYING BLOCK']=0,['AERODYNAMIC PROPELLER']=0,['WING']=0,['WING PANEL']=0}
simStart = function ()
    blocklist={}; infolist={}; bodlist={}
    mach = 'MACHINE'; findblock = GameObject.Find(mach)
    while (not findblock) do mach=mach..'(Clone)'; findblock = GameObject.Find(mach) end
    findblock = findblock.transform; blocknum = findblock.childCount
    for i=1,blocknum do
        blocklist[i]=findblock:GetChild(i-1)
        infolist[i]=blocklist[i]:GetComponent("MyBlockInfo")
        bodlist[i]=blocklist[i]:GetComponent("Rigidbody")
        if (not dragweird[infolist[i].blockname]) then bodlist[i].drag=2; bodlist[i].angularDrag=.5 end
        bodlist[i].useGravity=false
    end
end
boolint = function (a) if a then return 1 else return 0 end end
frame = function ()
    x = boolint(Input.GetKey(KeyCode.RightArrow))-boolint(Input.GetKey(KeyCode.LeftArrow))
    y = boolint(Input.GetKey(KeyCode.Z))-boolint(Input.GetKey(KeyCode.C))
    z = boolint(Input.GetKey(KeyCode.UpArrow))-boolint(Input.GetKey(KeyCode.DownArrow))
    hype = .05*math.sqrt(x*x+y*y+z*z+.0001); movevec = Vector3(x/hype, y/hype, z/hype)
    for i,j in ipairs(bodlist) do
        j:AddForce(movevec)
    end
end

EDIT: New, kind of completely different code! This one has a "plane" or "starfighter" effect instead of a "UFO" effect. That is, it has pitch, yaw, and roll controls instead of right/left, up/down, and forward/backward controls. It does have the downside that it will get "cranky" if too much of the craft falls off, and if the start block gets knocked off the main body it will spiral completely out of control. So...that start block is the pilot's seat! Protect it!

  • go forward: x
  • pitch up/down: up/down
  • yaw right/left: right/left
  • roll right/left: c/z
Once spaar adds scrollability to the in-game scripting mod, I'll put options at the beginning of the script so that even people who don't know how to code can modify the behavior of their hoverplane.

Be warned: this one is VERY laggy, even on small builds! On an non-gaming computer, this script renders the game unplayable on all but the very tiniest of builds.

Code:
import("UnityEngine")
blocklist={};infolist={};bodlist={};showtheway=GameObject.Find("bgeL0").transform
center = GameObject.Find("CamFollow").transform
dragweird={['FLYING BLOCK']=0,['AERODYNAMIC PROPELLER']=0,['WING']=0,['WING PANEL']=0}

simStart = function ()
    blocklist={}; infolist={}; bodlist={};showtheway=GameObject.Find("bgeL0").transform
    mach = 'MACHINE'; findblock = GameObject.Find(mach)
    while (not findblock) do mach=mach..'(Clone)'; findblock = GameObject.Find(mach) end
    findblock = findblock.transform; blocknum = findblock.childCount
    for i=1,blocknum do
        blocklist[i]=findblock:GetChild(i-1)
        infolist[i]=blocklist[i]:GetComponent("MyBlockInfo")
        bodlist[i]=blocklist[i]:GetComponent("Rigidbody")
        if (not dragweird[infolist[i].blockname]) then bodlist[i].drag=1.5; bodlist[i].angularDrag=.5 end
        bodlist[i].useGravity=false
    end
end

boolint = function (a) if a then return 1 else return 0 end end
subvec = function (a,b) return Vector3(a[0]-b[0],a[1]-b[1],a[2]-b[2]) end
multvec = function (a,b) return Vector3(a[0]*b,a[1]*b,a[2]*b) end
frame = function ()
    unif = ForceMode.Acceleration
    fwd = 100*boolint(Input.GetKey(KeyCode.X))
    x = 3*(boolint(Input.GetKey(KeyCode.DownArrow))-boolint(Input.GetKey(KeyCode.UpArrow)))
    y = 3*(boolint(Input.GetKey(KeyCode.RightArrow))-boolint(Input.GetKey(KeyCode.LeftArrow)))
    z = 5*(boolint(Input.GetKey(KeyCode.Z))-boolint(Input.GetKey(KeyCode.C)))
    gohere=multvec(showtheway.forward,fwd); turnhere=multvec(showtheway.up,y)
    tilthere=multvec(showtheway.right,x); spinhere=multvec(showtheway.forward,z)
    if fwd then for i,j in ipairs(bodlist) do j:AddForce(gohere, unif) end end
    if x then for i,j in ipairs(bodlist) do
        j:AddTorque(tilthere, unif); r = subvec(j.worldCenterOfMass,center.position)
        j:AddForce(Vector3.Cross(tilthere,r), unif) end end
    if y then for i,j in ipairs(bodlist) do
        j:AddTorque(turnhere, unif); r = subvec(j.worldCenterOfMass,center.position)
        j:AddForce(Vector3.Cross(turnhere,r), unif) end end
    if z then for i,j in ipairs(bodlist) do
        j:AddTorque(spinhere, unif); r = subvec(j.worldCenterOfMass,center.position)
        j:AddForce(Vector3.Cross(spinhere,r), unif) end end
end
 
#2
Second code added and topic name changed! I'd actually really appreciate it if someone is with a higher-end computer could try this out and tell me what kind of lag theyre getting, because what I'm getting is not exactly a frame rate drop; it's more like lua is running after Unity screaming "Wait! Wait for me!" so Unity has to turn back for half a second and wait for lua to catch up, and then Unity turns back around, but half a second later lua has fallen behind again.
 

NickLD

New Member
#4
You should consider making it a real mod in C# rather than Lua so it can preform better while using less resources. I think it could be a really cool Mod.
 
#5
NO! NEW THING BAD! CHANGE TERRIBLE! I STAY THE SAME, ALWAYS! KEEP NEW THING AWAYYY!

*sigh* yeah, i know, but I don't wanna have to deal with visualstudio, and "creating a project", and *hisses menacingly* 𝘮𝘢𝘯𝘥𝘢𝘵𝘰𝘳𝘺 𝘴𝘦𝘮𝘪𝘤𝘰𝘭𝘰𝘯𝘴.
I'll probably do it anyway, though.
Eventually.

EDIT: hmm, i wonder if I could...hmm. This may happen sooner than I thought
 

NickLD

New Member
#6
IF you want, I can port this for you into a real mod. Be really nice having a partner working on a mod. :D It gets lonely modding by yourself. T_T
 

ITR

l̺̤͈̘̰̺͉̳͉̖̝̱̻̠̦͈ͅ֍̫̜̥̭͖̱̟̟͉͙̜̰ͅl̺̤͈̘̰̺͉̳͉̖̝̱̻̠̦͈ͅ
Staff member
#7
We should make a steam-group for everybody who mods this, then everybody could be partners with each other :3
 

NickLD

New Member
#9
ITR that's a great idea! A steam group would really help coordinate modders so we can all work to something and help each other get work done!
 

ITR

l̺̤͈̘̰̺͉̳͉̖̝̱̻̠̦͈ͅ֍̫̜̥̭͖̱̟̟͉͙̜̰ͅl̺̤͈̘̰̺͉̳͉̖̝̱̻̠̦͈ͅ
Staff member
#10
But first we probably should all add each other, what's your steam IDs, Nick and stell?
 
Top