Ways to Unity 12: nested objects
Why we do nesting?
Because it allows us to move or rotate multiple objects as a whole.
It frees us from managing the relative movements between sub-objects manually.
How can we make a nested object
- Create an
empty game objectfrom theHierarchy window. - Drag and release your sub-objects to that
empty game object. - Make a script that does position or rotation transformations, then attach it to the
empty game object.
Example
If we put a Main Camera object under the empty game object. Then make the following script attached to the empty game object. By pressing the left or right key, you should see the camera rotates while always focusing on the empty game object:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateCamera : MonoBehaviour
{
public float rotateSpeed = 50;
void Start()
{
}
void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
transform.Rotate(Vector3.up, horizontalInput*rotateSpeed*Time.deltaTime);
}
}
The author
https://yingshaoxo.blogspot.com/2020/12/ways-to-unity-12-nested-objects.html