{"id":2281,"date":"2020-07-02T18:11:27","date_gmt":"2020-07-02T10:11:27","guid":{"rendered":"https:\/\/www.clonefactor.com\/wordpress\/?p=2281"},"modified":"2020-07-02T18:25:27","modified_gmt":"2020-07-02T10:25:27","slug":"put-shooting-marker-on-screen","status":"publish","type":"post","link":"https:\/\/www.clonefactor.com\/wordpress\/program\/c\/2281\/","title":{"rendered":"Put shooting marker on screen."},"content":{"rendered":"\n<p>When we want to make a on screen marker to identify the shooting target on screen.<br \/>basically we asking &#8220;how to convert World position into UGUI screen position in term of RectTransform anchor.&#8221;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2282\" src=\"https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2020\/07\/MarkerOnScreen.jpg\" alt=\"\" width=\"1033\" height=\"942\" srcset=\"https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2020\/07\/MarkerOnScreen.jpg 1033w, https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2020\/07\/MarkerOnScreen-300x274.jpg 300w, https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2020\/07\/MarkerOnScreen-1024x934.jpg 1024w, https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2020\/07\/MarkerOnScreen-768x700.jpg 768w, https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2020\/07\/MarkerOnScreen-329x300.jpg 329w\" sizes=\"auto, (max-width: 1033px) 100vw, 1033px\" \/><\/p>\n<p>Technically speaking it&#8217;s a space convert issue.<br \/>in this case assume we following Object in the scene :<\/p>\n<ol>\n<li><strong>m_WorldPosition<\/strong> : the target position we wanted to display but in 3D world space (perhaps the raycast hit point?!)<\/li>\n<li><strong>m_ViewCamera<\/strong> : a camera for player to explore virtual world. (upper camera)<\/li>\n<li><strong>m_UICamera<\/strong> : a camera only culling UI overlayer (camera only for UI)<\/li>\n<li><strong>m_MarkerRect<\/strong> : the RectTransform based on UGUI Canvas. (the MARKER)<\/li>\n<\/ol>\n<p>so here is what we wanted to do :<\/p>\n<ol>\n<li><span style=\"font-size: inherit; text-indent: 0em;\">first we need to convert the world position (<\/span><strong style=\"font-size: inherit; text-indent: 0em;\">m_WorldPosition<\/strong><span style=\"font-size: inherit; text-indent: 0em;\">) from &#8220;<strong>m_ViewCamera<\/strong>&#8221; into the screen position<\/span><\/li>\n<li>and then use that screen position based on &#8220;<strong>m_UICamera<\/strong>&#8221; matrix, convert onto UGUI RectTransform anchorPosition<\/li>\n<\/ol>\n<p>so the code will look like this.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/ assume we had following\n\/\/ RectTransform m_MarkerRect;\n\/\/ Camera m_ViewCamera;\n\/\/ Camera m_UICamera;\n\/\/ Vector3 m_WorldPosition;\n\nVector3 screenPos = m_ViewCamera.WorldToScreenPoint(m_WorldPosition);\nbool withInParentRect = RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)m_MarkerRect.parent, screenPos, m_UICamera, out Vector2 anchorPos);<\/pre>\n<p>we used following API.<\/p>\n<ul>\n<li><a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/Camera.WorldToScreenPoint.html\">Camera.WorldToScreenPoint<\/a><\/li>\n<li><a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/RectTransformUtility.ScreenPointToLocalPointInRectangle.html\">RectTransformUtility.ScreenPointToLocalPointInRectangle<\/a><br \/>Notes: by using the &#8220;RectTransform.anchoredPosition&#8221; we can define the center of the image or what ever you wanted for the UI&#8217;s pivot poition.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\u00a0<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When we want to make a on screen marker to identif &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,78],"tags":[17,62,89],"class_list":["post-2281","post","type-post","status-publish","format-standard","hentry","category-c","category-math","tag-camera","tag-coordinate","tag-math"],"_links":{"self":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/2281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/comments?post=2281"}],"version-history":[{"count":2,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/2281\/revisions"}],"predecessor-version":[{"id":2284,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/2281\/revisions\/2284"}],"wp:attachment":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/media?parent=2281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/categories?post=2281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/tags?post=2281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}