Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize 3D coordinate Transformation into UGUI coordinate in Unity

Shulou Source: shulou.com Published: 2022-06-01 05:11:33 10月04日 Update

This article mainly shows you "how to convert 3D coordinates into UGUI coordinates in Unity". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to convert 3D coordinates into UGUI coordinates in Unity".

0. Preface

In actual development, we often encounter problems that require 3D and 2D coordinate conversion, such as following the movement of a character synchronously, the number of damage appears on the character's head, and dragging a UI from a 2D backpack to a 3D object, which are essentially problems of 3D coordinate and UI coordinate conversion.

1. Core thought

To convert 3D coordinates to UI coordinates is to convert them into screen coordinates first, and then to convert them into UI coordinates.

So the coordinate transformation is divided into three parts.

3D coordinate

Screen coordinates

UI coordinates

The screen coordinate system of Unity stipulates that the lower left corner is the origin, the X axis is positive to the right, and the Y axis is positive.

As an additional knowledge point, the coordinate system of GUI stipulates that the upper left corner is the origin, the X axis is positive to the right, and the Y axis is positive to the right, which is different from the screen coordinates, but we don't need this knowledge point in this article.

Here the UI coordinate refers to the UGUI coordinate, so we need to use the Canvas-related parameters when calculating.

two。 Code example

Here, it takes the blood strip to follow the character movement as the requirement, and completes the core code of coordinate transformation.

Using UnityEngine

Public class HPFollow: MonoBehaviour {/ / public Transform heroHeadTrans; / / camera on top of a character. Camera.main is not used because of the high performance consumption of public Camera mainCamera.

/ / Blood strips public RectTransform hpTrans

/ / Canvas public RectTransform canvasTrans

/ / Update is called once per frame void Update () {/ / convert 3D coordinates to screen coordinates Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint (mainCamera, heroHeadTrans.position) first

/ / then convert the screen coordinates to UGUI coordinates Vector2 localPoint

If (RectTransformUtility.ScreenPointToLocalPointInRectangle (canvasTrans, screenPoint, mainCamera, out localPoint)) {hpTrans.anchoredPosition = localPoint;}}

Don't forget to modify Canvas's RenderMode.

These are all the contents of the article "how to transform 3D coordinates into UGUI coordinates in Unity". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Coordinates screen character direction content knowledge article code origin coordinate system core knowledge point problem learning help move three preface parameter head Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi macOS Shulou Information Shulou Technology MySQL