Bouton Quit

Quit button

This tutorial will teach you how to make a button. This button will simply quit your game when you click on it.

First, you need a button model. I will assume you know how to do it, this is not the point of this course. I will use this one, coming from "Doomsday Carrot Rampage".

Once you have your model, create a scene, put a camera (called "camera") object in it and an object with a model renderer with your button as model.

Step-by-step scripting Edit

Now, we need our button to be "clickable".

Create a new script, you should get this:

function Behavior:Awake()

function Behavior:Update()

Behavior:Awake() is called only once when the game is launched. In this function we will initialize some variable used to acces our objects easily later.

Firstly, we need to get the model renderer belonging to our button object, we will create a variable called "modelRndr". Assuming our script is attached to it, we acces the object with "self.gameObject" and we need his component "ModelRenderer" :

self.modelRndr = self.gameObject:GetComponent("ModelRenderer" )

Then, in the same way, we need the "Camera" component from our "camera" object. = CraftStudio.FindGameObject( "camera" ):GetComponent( "Camera" )

So we have our Behavion:Awake() function :

​function Behavior:Awake()

self.modelRndr = self.gameObject:GetComponent("ModelRenderer" ) = CraftStudio.FindGameObject( "camera" ):GetComponent( "Camera" )


Behavior:Update()? is called one time per frame, so 60 time per second. This function wil continusly check if the user click on the button.

In the administration tab of your project, in the control tab, create a control called "Fire" link to the mouse right clic. So, we have to check if the button "Fire" have been pressed :

if CraftStudio.Input.WasButtonJustPressed("Fire")then


If is a conditinal statement, every thing between "then" and "end" will be run only if our condition is true.

Then, we have to check if the mouse cursor is on the button. To do it, we will cast a ray from our camera to our cursor.

To create this ray, we need the actual position of our cursor. We can get it with :

local mousePos = CraftStudio.Input.GetMousePosition()

Our variable "mousePos" will contain our cursor position.

Now, we can create our ray :

local ray = mousePos )

Our variable "ray", contain a ray starting at the camera position and ? pass? through? the cursor.

Lastly, we check if the ray pass? through our model. We make an other condition nested in the first one :

if ray:IntersectsModelRenderer( self.modelRndr ) ~= nil then


ray:IntersectsModelRenderer( self.modelRndr )? is our conditionest. This function will return the distance between the starting point of our ray and the intersection point with "self.modelRndr". If there is no intersection, the fubnction return "nil".

The symbol "~=" means "different of".

Nil is the value for an empty varaible.

So, we can translate our "if" statement by: "If the intersection distance is not nul (the intersection exist so the ray touch the buton)"

The last used function, is used to quit our game :


I think it seem obvious.

Now, return in your scene, and add a script component to the button object then select your script.

The whole script Edit

Here is our final script :

function Behavior:Awake() = CraftStudio.FindGameObject( "camera" ):GetComponent( "Camera" )
    self.modelRndr = self.gameObject:GetComponent( "ModelRenderer" )

function Behavior:Update()
    if CraftStudio.Input.WasButtonJustPressed("clicgauche") then
        local mousePos = CraftStudio.Input.GetMousePosition()
        local ray = mousePos )
        local distance = ray:IntersectsModelRenderer( self.modelRndr )
        if ray:IntersectsModelRenderer( self.modelRndr ) ~= nil then

I hope this tutorial is clear enough and teaches you something!

Wardow (translated from french by Jaunmakenro)

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.