Analog input detection in Lua

Crayta provides a Lua input API so that scripts can detect key/button presses, such as jump, crouch, sprint, and extra actions. However, the analog inputs (move and look around - WASD and mouse on keyboard/mouse, or left and right sticks on controller) currently only control the player’s character - these inputs can’t be detected in Lua.

This feature request suggests adding Lua functionality to detect the left and right analog inputs, probably as Vector2Ds. It needs to be considered that the Lua must detect both keyboard and mouse, and controller inputs, in the same way.

As well as detecting the left and right inputs, this request suggests being able to detect the pressure of the primary and secondary triggers, as a float value between 0 and 1. This would need to just return a binary 0 or 1 for keyboard and mouse users though, to represent whether the primary or secondary mouse button is held.

Detecting these analog inputs would allow creators to give players a lot more control within games - it could be the start of creating a vehicle system for an advanced creator, but would also allow for custom camera controls, or the ability to move any objects other than the player, such as sliding a block in a puzzle mini-game.

Votes on original board: 13


suggest as pan_r pan_l pan_up pan_down

I kinda managed to get those inputs for Acryptica. But it is hacky and limited. Could make into package, but not sure how long it will work :slight_smile:

The trick works like this - firstly you have to use orbit camera and be ok that it would be fixed at a certain angle. Secondly - you can get X and Y using this method, but they would be on the client. How to send this to server - might depend on your use case.
Now you should NOT lock orbit camera angle by option in menu, but rather use OnLocalTick and just check properties cameraYaw and cameraPitch. If there is a difference from whatever static values you want to fix, reset those properties, but remember the difference. diffYaw and diffPitch then would basically be your diffX and diffY (with some normalization for speed - play around with different coefficients) - add them to 2d vector, that you should keep in the table on client side - you will basically receive an integral. Next step woud be to bound this vector. In Acryptica I simply normalize it to 1, because I only need direction, not absolute position, but for other purposes you might want to do different stuff with it, like for example lock it from going too far, like normal mouse pointer does, or maybe roll it in the tor, whatever feels more appropriate.

Next is the part that very depends on your situation. Maybe keeping completely on client side is enough. But maybe you need the value on server at a certain point. On Acryptica I needed the direction where “mouse” or stick was pointing, when spell is casted. So I simply added one message from server and back, which only happens when player casts a spell. Rotation of spell pointer ( red light ) is only changing on the client, since other players do not need to see your pointer, and this looks very smoothly. Spells are casted with cooldown, so there is not much additional stuff going from server and back.

Seems kinda long, but hopefully will help someone…

You can check how it works in Acryptica: Crayta