In this document, you will learn the core construct of iCanScript by creating a visual script to move a sphere. We assume you have already installed iCanScript and you have opened a new Unity scene.
1- Open the iCanScript Editors
iCanScript comes with the following editor windows:
- Visual Editor (main edition window);
- Library Tree (from where you get your nodes);
- Instance Wizard (object-oriented panel to work with object instances);
- Tree View (same as Visual Editor but in tree format);
- User Preferences Panel (to personalize the look and behaviour).
Open the Visual Editor, the Library Tree, the Instance Wizard and the Tree View and arrange the layout to your liking.
2- Create a Sphere Component
Use the menu GameObject->Create Other->Sphere to create the sphere object that will host the visual script.
3- Add the Visual Script to the Sphere
A Visual Script is a component that needs to be added to a game object:
- Select the Sphere game object and;
- Create the Visual Script using Component->iCanScript->Visual Script.
The Visual Editor will now display a canvas and is now ready to be populated with nodes.
4- Every Script Starts With a Message Handler…
Every script in Unity is invoked from a message sent by the Unity framework. Use the following steps to create the Update message handler for your visual script:
- Right click (or two finger click for Mac users) on the canvas of the Visual Editor;
- Select from the menu that appears the “+ Update”
The Visual Script now contains a message handler that will execute when Unity send the Update message.
5- Accessing the Sphere Transform
Moving the sphere requires that you modify its transform component. Before you can do so, you will need to create a node representing its transform.
- Click and drag the transform port from the edge into the Update node;
- Release the port inside the Update node;
- Select “+ Object Instance” from the menu to create a transform instance node.
Have you noticed that the instance port (i.e. “this” port) is connected to the Update::transform port. This type of connection is called a binding.
6- Move the Sphere
Add the translate function to your transform instance node using the Instance Wizard editor by:
- Selecting the Transform instance node;
- This will display the available variables and operations in the Instance Wizard.
- Scroll the Operations section of the Instance Wizard until you see “Translate(x:float, y:float, z:float)”;
- Click the “Translate(x:float, y:float, z:float)” to add the function to the node.
In this example, you will move the sphere in the X-Axis only with constant velocity. This can be realized by configuring the input ports of the translate function of the transform:
- Select the x.translate port of the Transform node;
- In the Inspector, open the iCS_Visual Script component;
- Modify the x.Translate attribute with the value of 0.2 (displacement/frame).
7- Run the Visual Script
Your visual script is now ready to run. Just press the run button and watch your sphere move in the x-axis.
8- What Have You Learned?
- Setup your environment to use iCanScript editors;
- Install a Visual Script;
- Create a message handler node to respond to Unity Update messages;
- Extract the transform instance node from the game object;
- Populate the transform node with the translate(…) function using the Instance Wizard;
- Configure the translate(…) function to move the sphere;
- Run the visual script.