top of page
Writer's pictureDrew Nelson

Implementing Unity3D's Object Hierarchy and Camera Control

Updated: Dec 16, 2020

Unity3D and many other game engines provide game designers this concept of an object hierarchy. Simply put, game designers are able to nest objects under one another. The child object's position, rotation, and scale is then affected by the parent's transformation. This simple concept can help game designers when creating behaviors in which objects depend on one another. For example, one could easily create the behavior of the hand on a analog clock. By nesting the hand on an empty game object, you could then rotate the empty game object to give it that natural rotating feel:



The goal of this project was to recreate this functionality of the object hierarchy. Additionally, to help observe the scene, some of the camera controls that are normally used in the Unity editor were created.


Object Hierarchy

In order to achieve this functionality, I needed to calculate the the resulting transform of each object in the scene based the object's parent's position. If the parent object has a parent as well, this would also need to be included in the calculation. This process would repeat until we've reached our starting object.


Using Unity3D's Matrix4x4 and Vector3 classes, we were able to compute all the calculations needed. In Unity, when using the default material, the engine will determine the position of each object and load those positions into the default shader. To help bypass this, a custom shader was created that allowed us to pass the resulting transform for each object into the shader ourselves.


To help create the desired hierarchy, the concept of nodes and primitives were introduced. The nodes acts as containers that have an initial position and a collection of primitives. The primitives are the objects actually rendered in the scene based on the node it is a part of. Nodes could have child node under them, but do note - they are not required to be nested in the unity scene hierarchy view. In this project, users are able to modify the transforms of the nodes. All of the nodes in the scene already have primitives attached to them with some rotational movement. As the user moves the scene nodes, the primitives should maintain that positional relationship as if they were nested objects in Unity's object hierarchy. In the example below, you can see that the pink cube stays on top of the elongated sphere as we rotate the node:



Camera Control

As we transform our scene nodes, viewing some of the results might be difficult. To help with this, the requirement of implementing some camera functionality was added. In this scene, camera tracking, camera tumble, and camera zooming was added.


Additionally, to understand view ports, a secondary camera was added to the scene. This camera provide the view of the one of the nodes in our object structure. In the image below, you can see a white cylinder extending out which indicates the viewing direction of the secondary camera.



This viewing direction of the secondary camera is update as the parent node's transform is updated.


A short demo of the project can be seen here:


Languages and Tools Used:

Unity3D, C#, HLSL

8 views0 comments

Recent Posts

See All

Comments


bottom of page