Written by bilou84, Translation edited by NotExplosive Hello everyone! Welcome to my first tutorial. I hope this to be the first of many!

For those who don't know me, I participate in the creation of: Porte par le Vent, Doomsday Carrot Rampage and CraftFighter. My speciality is scripting, which is what this tutorial is about.

My goal is to show you how to script with CraftStudio. I mean helping you to understand how the API works and how to use it.

I won't explain everything from the beginning. So please check out the wiki and games thats have already been made.

Getting StartedEdit

So you just finished off your awesome character. You're happy but now you have the question of how to make it move?

Well, it's not that complicated.

local MOVE_SPEED = 0.1 
local ROTATE_SPEED = 2 

function Behavior:Awake() 


function Behavior:Update() 
    local horizontal = CraftStudio.Input.GetAxisValue( "Horizontal" ) 
    local vertical = CraftStudio.Input.GetAxisValue( "Vertical" ) 

    self.gameObject.transform:Rotate( Quaternion:FromAxisAngle( Vector3:Up(), horizontal*ROTATE_SPEED) )
    self.gameObject.transform:MoveOriented( vertical*Vector3:Forward()*MOVE_SPEED ) 

To make it work, you will have to define two axes on the administration tab: horizontal and vertical. For horizontal : Q (or A for QWERTY keyboards) for positive and D for negative. For vertical: Z for positive and S for negative.

Then you just have to attach that script to the object you want to move.

I will use Q and D to rotate your object. Z and S to move it.

Okay. So now, how does it work ?

At the beginning, we have :

local MOVE_SPEED = 0.1
local ROTATE_SPEED = 2

These two are constants, which make your script easier to read. You must avoid having stray numbers that you don't understand the meaning. You could just put "0.1" in every place where you wrote "MOVE_SPEED" in the script to save yourself this step, however then you would have to change the "0.1" in every spot it was used if you ever wanted to change it, and also other people reading your code may not understand it.

Name your constants appropriately to what they do!

Next we have the Awake which is empty. We don't need anything particular for this code since everything we're going to be doing will happen in a loop, so since? Awake? only happens once, we don't have a use for it.

Finally the Update :

local horizontal = CraftStudio.Input.GetAxisValue( "Horizontal" )
local vertical = CraftStudio.Input.GetAxisValue( "Vertical" )

We use these lines to check if you pressed the keys. For instance, when you pressed Z, vertical will become 1.


self.gameObject.transform:Rotate( Quaternion:FromAxisAngle( Vector3:Up(), horizontal*ROTATE_SPEED) )

This line make your object rotate.

self refers to the object the script is attached to. gameObject give acces to the object itself. transform allow you to use the attributes toward the displacement of the object.

First we will use Rotate. This one make your object rotate, given a Quaternion in parameters. A Quaternion is a weird object which reflects a rotation. You can create it from an axis and the magnitude of the rotation with the attribut FromAxisAngle. Here we use the vertical axis with Vector3:Up() and we have already saw the rotation magnitude with ROTATE_SPEED.

We can also see that the constant was multiplied by horizontal. We don't want to make our player move constantly. So when we don't press the key, horizontal equal to 0, so the net movement evaluates to 0.

Last line :

self.gameObject.transform:MoveOriented( vertical*Vector3:Forward()*MOVE_SPEED )

The beginning is the same but this time we use the attribut MoveOriented.

With this script, you can move your object according to its orientation. It uses the Vector3:Forward() to move forward. Once more, there is the constant which contains the speed and it is multiplied by vertical in order to move when you press a button.


Done! I hope my explanations are clear enough and my english as well. (I'm French)

You can see here a video to illustrate this tutorial : Video ? It's in french but there are subtitles.

And here is a link to the project source if you want to test it by yourself:? Pack

Thanks you for reading and feel free to leave comments !


Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.