Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

Unity/Transforms and hierarchies

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

Introduction to Transforms In Unity, the Transform component is the cornerstone of every GameObject. It's the fundamental piece that defines an object's position, rotation, and scale within the 3D space of your scene. Without a Transform, a GameObject wouldn't have a place or orientation in the world—it's essentially the DNA that gives life to everything you create. Understanding the Transform Component Every GameObject you create in Unity automatically comes with a Transform component. This isn't just a coincidence; it's because the Transform is essential for Unity to know where and how to display the object in the scene. The transform have 3 component: position, rotation and scale The Transform as the Bridge Between GameObjects and the World The Transform isn't just about numbers; it's how your GameObjects interact with the entire scene. Spatial Relationships: Determines how objects relate to each other in space. For instance, a character standing next to a building needs accurate positioning to avoid clipping or floating. Collisions and Physics: The physics engine relies on Transforms to calculate collisions, forces, and other physical interactions. Coordinate Systems in Unity Parent and Child One of the Transform's powerful features is its ability to establish parent-child relationships between GameObjects. Parenting: When one GameObject is made the parent of another, the child inherits the parent's Transform properties. Moving the parent moves the child. Rotating the parent rotates the child relative to the parent. Local vs. World Space World Space The global coordinate system of the scene. Positions, rotations, and scales are relative to the world's origin point (0,0,0). Useful when you need an object to interact with the scene regardless of its hierarchy. Local Space The object's own coordinate system, influenced by its parent's Transform. Positions, rotations, and scales are relative to the parent GameObject. Essential when working with hierarchies, so child objects behave correctly in relation to their parents. Understanding Transform Order of Operations The order in which position, rotation, and scale are applied can affect the final result. Scale is applied first, then rotation, and finally position. This means scaling can influence how rotation operates, which can sometimes lead to unexpected results. e.g. Scaling a child object negatively (e.g., flipping it) can invert its rotation relative to the parent. Scripting with Transforms The Transform component can be accessed and manipulated through code, allowing dynamic adjustments at runtime. public class MoveObject : MonoBehaviour { void Update() { // Move the object upward at 1 unit per second. transform.position += Vector3.up * Time.deltaTime; } } Common Transform Properties and Methods: transform.position: Get or set the object's position. transform.rotation: Get or set the object's rotation. transform.localScale: Get or set the object's scale. transform.Translate(): Move the object in space. transform.Rotate(): Rotate the object around an axis. transform.LookAt(): Make the object face a specific point. Transform Parenting at Runtime You can change parent objects during gameplay. Useful for attaching and detaching objects dynamically. public void AttachTo(GameObject newParent) { transform.SetParent(newParent.transform); } Transform Direction Vectors Use transform.forward, transform.up, and transform.right to get directional vectors relative to the object. Moving an object forward: transform.position += transform.forward * speed * Time.deltaTime;