Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of U3D Local coordinates and Global coordinates

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail the example analysis of U3D local coordinates and global coordinates. Xiaobian thinks it is quite practical, so share it with you for reference. I hope you can gain something after reading this article.

Father-son transformation (extracted from the original description of the official text)

Parenting is one of the most important concepts to understand when working with Unity. When a GameObject is the parent of another GameObject, the Child GameObject moves, rotates, and scales like its parent. Just as your arms are connected to your torso, when you rotate your torso, your arms move because they are connected to you. Any object can have multiple children, but only one parent.

You can create a Parent by dragging any GameObject in Hierarchy View onto another GameObject. This creates a parent-child relationship between two GameObjects.

Example of a parent-child hierarchy view. GameObjects with folded arrows to the left of their names are parents.

In the example above, we think of the torso as the parent of the arm and the arm as the parent of the hand. The scene you make in Unity will contain a collection of these Transform hierarchies. The topmost parent object is called the root object. When you move, scale, or rotate a parent object, all changes in its Transform are applied to its children.

It is worth noting that the Transform values in the Inspector of any Child GameObject are displayed relative to the Transform values of the Parent. These values are also called local coordinates. You can access global coordinates and local coordinates through scripting.

For example:

Cube1 coordinates are treated as fully local or world coordinates

gameObject.transform.position=new Vector(2,2,0);

may also be used

Vector3 thePosition = transform.TransformPoint(newVector3(2,2,0));

gameObject.transform.position = thePosition;

Transform.TransformPoint: Transforms position from local space to world space.

Now add Cube1 as a child Cube2 and move Cube2 to the position shown.

gameObject.transform.position=new Vector(2,2,0); will not work;

Problem analysis: According to the red text, the coordinates of the Inspector in Cube2 are displayed relative to the coordinates of the parent Cube1, so you can only use the transform.TransformPoint method to convert your own coordinates into world coordinates.

Vector3 thePosition = transform.TransformPoint(newVector3(2,2.5,0));

gameObject.transform.position = thePosition;

Transformation of coordinate systems:

TransformDirection Transforms a direction from the local coordinate system to the world coordinate system

InverseTransformDirection converts a direction from world coordinate system to local coordinate system

TransformPoint Transforms a point from the local coordinate system to the world coordinate system

InverseTransformPoint converts a point from world coordinate system to local coordinate system

About "U3D local coordinates and global coordinates example analysis" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report