Boromir the Segway [In-Game Scripting]

#1
A working segway I made using Spaar's In-Game Scripting mod! Some credit also goes to =Ø= for asking a few important questions before I could.

Controls are just the cursor keys; note that the forward/backward accelerate Borromir in that direction instead of quickly changing the his speed to some predefined value, and his speed will not return to 0 when the keys are released. I plan to fix this, as well as much of the wobbliness, very soon.





Code:
import ('UnityEngine')
import ('math')
vWheel = 0.0 -- speed of the wheel
oldTilt = 0.0 -- the angle of the center block on the previous tick. will be used in future versions
gyro = GameObject.Find('bgeL0').transform -- the position of the center block, the gyroscope
tick = 0 -- used to lower amount of messages to console

simStart = function ()
  vWheel = 0.0
  oldTilt = 0.0
  gyro = GameObject.Find('bgeL0')
  tick = 0
end

frame = function ()
  -- get the angle of the gyroscope and make sure it's between 180 and -180
  reading = gyro.transform.eulerAngles[2]
  if (reading > 180) then reading = reading - 360 end
  -- set the velocity of the wheel to counteract the tilt. The speed of the wheel is less as it gets closer
  -- to upright in order to keep it from oscillating back and forth too badly.
  -- the code in real segways is a bit more complicated, but I wanted to post this as soon as I got
  -- it working at all
  vWheel = reading/10
  -- every 5 ticks, log relevant stuff
  tick = tick + 1
  if (tick == 5) then
    Debug.Log(vWheel)
    tick = 0
  end
end
Note: Boromir is only technically named after LoTR character; I needed a name for him, and Boromir was a preexisting name that fit very well.

Attached Files
boromir.bsg
 
Top