Thank you for taking part in the NotSlot journey!
We invite you to join us in our next chapter:
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.
When working with Game Objects, we want to move and change them by accessing the Transform component’s properties.
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.
We can use the Get Lossy Scale node to get the Object’s actual size.
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.
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.
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.
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.
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:
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.
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.
Another valuable node is Look At. With this node, we can rotate to follow another Game Object as it moves.
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.
We can get the direction axis the Transform faces with the Get Up, Get Right, and 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!