2019 2022

Thank you for taking part in the NotSlot journey!
We invite you to join us in our next chapter:

  • by nir,
  • Danielle Elias

Visual Scripting Components Part 1: Transform

In this article, we will get to know how we can move, rotate and scale Game Objects in Unity’s Visual Scripting. We’ll explore all the nodes Unity offers for working with the Transform component.

Properties

When working with Game Objects, we want to move and change them by accessing the Transform component’s properties.

Position

To access the position of a Game Object in the world, we use the Get Position node. To modify it, we’ll use the Set Position node.

Unity Visual Scripting – Get Position, Set Position
Get/Set Position nodes.

Scale

We can use the Get Lossy Scale node to get the Object’s actual size.

Unity Visual Scripting – Get Loosty Scale
Get Loosty Scale node.

Rotation

When we want to rotate an Object, we have the Get Euler Angles & Set Euler Angles nodes; we can retrieve and modify the Object’s rotation angle through them.

Unity Visual Scripting – Get/Set Euler Angles
Get/Set Euler Angles nodes.

If we want to increase or decrease the existing rotation angle, we can use the Transform Rotate node. It also allows us to configure the Relative To option, which we can set to World or Self – determining whether to rotate the Object relative to itself or to the parent Game Object it is under.

Unity Visual Scripting – Rotate
Rotate node.

Parenting

When we nest Objects in the hierarchy, we refer to them as a parent and child.
We have access to various nodes dedicated to controlling the Objects’ hierarchy.
In some cases, we will set a Game Object as a child affected by its parent, such as the case of a tank’s turret. We want the turret to rotate and move along with the tank’s body, so we’ll set it as a child of the body Object.

In the graph, we use the Get Parent & Set Parent nodes to control the Object’s parent and retrieve it.
We got another node for setting the parent, Set Parent (World Position Stays) node, and with it, as its name suggests, we can choose to retain the Object’s world position, rotation, and scale when changing the parent.

Unity Visual Scripting – Get/Set Parnet
Get/Set Parnet nodes.

A parent Object may have multiple children; we can ask for a specific child using the Get Child node or count them with the Get Child Count node.
Combing these nodes with the For Loop node, we access all the parent’s children.
The Detach Children node allows us to disconnect all the parent’s child Game Objects.

Unity Visual Scripting – Get Child, Get Child Count, Detach Children
Get Child, Get Child Count, Detach Children nodes.

Local vs World

We already know we can get and set the different properties of the Transform component; we can also transform Game Objects relative to their parent.

We can use nodes similar to those we met earlier:

  • Get/Set Local Position
  • Get/Set Local Scale
  • Get/Set Local Euler Angles

These nodes differ from the previous nodes we learned about in such they are retrieving or changing the properties based on the parent. For example, Get Local Position provides us the distance from the parent instead of the position in the world.

Unity Visual Scripting – Transform local
Transform local nodes.

Moving Objects

Moving a Game Object is a fundamental part of creating any game in Unity, and as such, there are many ways to do so.
The Translate node moves a Game Object in the direction and distance based on the vector input. It’s a shortcut for a graph built of Get Position and Add nodes connected to Set Position.

Unity Visual Scripting – Translate
Translate node.

Another valuable node is Look At. With this node, we can rotate to follow another Game Object as it moves.

Unity Visual Scripting – Look At
Look At node.

For example, we can make the game’s camera follow another Game Object.
We add a Script Graph to the camera Game Object and connect the Update event to the Look At node. We attach the Game Object to the Target input using the Game Object Find node. When we move the Game Object at runtime, the camera will follow it around.

Movement Direction

We can get the direction axis the Transform faces with the Get Up, Get Right, and Get Forward nodes:

  • Get Up is for the green arrow.
  • Get Right is for the red arrow.
  • Get Forward is for the blue arrow.
Unity Visual Scripting – Transform Get Up, Get Right, Get Forward
Get Up, Get Right, Get Forward nodes.

When it comes to controlling the movement direction of a Game Object, these nodes come in handy.
For example, connecting the Get Up node to the Translate node will move the Object one unit in the direction of the green arrow.


Thank you for following along!

© All rights reserved.