This is an example, screenshot of which I've posted on Twitter some time ago but was a bit busy to make illustrated guide and post for. It's about creating a 3d platformer game in CraftStudio via recently introduced physics functions.
Since one of recent versions, CraftStudio includes Bullet physics engine, which allows games to include physics-based functionality with a number of primitive shape types. In conjunction with existing functions for raycasting, this can be used to create a range of games without making any custom collision implementations.
Player behaviour would be the hardest thing to make out of anything featured in this example,
(click the image for full-size version)
But is not that complicated actually.
"Awake" event is used to set up a number of local variables that will determine movement parameters. "traceLength" here is distance from player' center-of-mass to bottom of feet (can be measured in editor).
"Update" event is a bit more complicated. First we create local variables to hold a reference to physics object (for ease of use) and camera rotation vector (containing angles of rotation around X/Y/Z axis).
Afterwards goes the horizontal (XZ plane) movement. That's done by making a vector with movement axis values (-1 to +1 range), multiplying it by walking speed, and rotating around Y axis (via quarterion) to match with camera angle correctly. Resulting vector is simply applied to physics object as force delta.
Jumping goes slightly different - when jump key is pressed, a ray is sent downwards to find distance till nearest obstacle. If distance is less or equal to traceLength, it means that character is standing on the ground, thus may jump. Jumping itself is done by applying an impulse to physics object.
There's also a pseudo-shadow-casting behaviour in this example, which may come handy:
(click the image for full-size version)
It works as following:
On creation, initial shadow opacity is saved. This is needed because shadow should be hidden when player jumps over bottomless pits of sorts.
On each update, a ray is being cast from player position downwards. Ray trace distance is afterwards used to determine shadow position and it's size ("^" operator is used to raise numbers to powers, in this case slowly shrinking the shadow as player gets further from the ground). Shadow is hidden (alpha set to zero) if ray does not hit anything.
You can also download all related files for this example (note that you will need to change project startup scene from Administration tab if you want to run this):
Shouldn’t you be setting variables instead of creating them in the update ?
Technically “create a local variable” is mapped to “local” keyword of Lua, which would be “var” or “dim” in other programming languages. While it is possible to eliminate all scope-specific variables in favour of fields, this only increases complexity of example, while not gaining noticeable benefit (since player is normally present in a single instance through the game).