{"id":78,"date":"2020-06-03T01:56:07","date_gmt":"2020-06-03T01:56:07","guid":{"rendered":"https:\/\/lucyestela.com\/dev\/?p=78"},"modified":"2021-03-02T03:01:32","modified_gmt":"2021-03-02T03:01:32","slug":"transforms","status":"publish","type":"post","link":"https:\/\/lucyestela.com\/dev\/unity\/transforms\/","title":{"rendered":"Transforms &#8211; Unity"},"content":{"rendered":"<div class=\"boldgrid-section\">\n<div class=\"container\">\n<div class=\"row\">\n<div class=\"col-md-12 col-xs-12 col-sm-12\">\n<p><strong>Transform.LookAt<\/strong><\/p>\n<pre class=\"\">public void <span class=\"sig-kw\">LookAt<\/span>(<a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/Transform.html\">Transform<\/a>&nbsp;<span class=\"sig-kw\">target<\/span>,&nbsp;<a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/Vector3.html\">Vector3<\/a>&nbsp;<span class=\"sig-kw\">worldUp<\/span>&nbsp;= Vector3.up);<\/pre>\n<p class=\"subsection\">Rotates the transform so the forward vector points at \/target\/&#8217;s current position.<\/p>\n<p class=\"subsection\">Then it rotates the transform to point its up direction vector in the direction hinted at by the&nbsp;<code class=\"varname\">worldUp<\/code>&nbsp;vector. If you leave out the&nbsp;<code class=\"varname\">worldUp<\/code>&nbsp;parameter, the function will use the world y axis.&nbsp;<\/p>\n<p class=\"\">So if I want the gameObject to look at the mouse pointer in a 2D top down game, I could use the following code to rotate the gameObject.<\/p>\n<pre class=\"\"><span style=\"font-family: Menlo;\"><span style=\"color: #222222;\"><span style=\"color: #009695;\">private<\/span>&nbsp;Vector3&nbsp;mousePoint3D;&nbsp;<\/span><\/span>\r\n\r\n<span style=\"font-family: Menlo;\"><span style=\"color: #222222;\">mousePoint3D<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">=<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">Camera<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">main<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">ScreenToWorldPoint<\/span><span style=\"color: #222222;\">(<\/span><span style=\"color: #222222;\">Input<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">mousePosition<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">+<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">Vector3<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">back<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">*<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">Camera<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">main<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">transform<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">position<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">z<\/span><span style=\"color: #222222;\">);<\/span><\/span>\r\n\r\n<span style=\"color: #222222;\">transform<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">LookAt<\/span><span style=\"color: #222222;\">(<\/span><span style=\"color: #222222;\">mousePoint3D<\/span><span style=\"color: #222222;\">,<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">Vector3<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">back<\/span><span style=\"color: #222222;\">);<\/span><\/pre>\n<p class=\"\">&nbsp;<\/p>\n<p><strong>Transform.SetParent<\/strong><\/p>\n<p class=\"\">If you have an app where you will need to instantiate a gameobject as a child of another gameobject, then you will need to use Transform.SetParent to do this. Unity actually does a nice document on explaining this <a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/Transform.SetParent.html\">here.&nbsp; <\/a>But in case you don&#8217;t want to link through, here is the main bit of the code you need.<\/p>\n<p class=\"\">define your child which is a game object and the transform of the desired parent gameobject clip&nbsp;<\/p>\n<pre class=\"\"><span style=\"font-family: Menlo;\"><span style=\"color: #009695;\">public<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">GameObject<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">child<\/span><span style=\"color: #222222;\">;<\/span>\r\n<span style=\"color: #009695;\">public<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">Transform<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">parent<\/span><span style=\"color: #222222;\">;<\/span><\/span><\/pre>\n<p>Then you assign\/remove the child and parent in the following ways<\/p>\n<pre class=\"\"><span style=\"font-family: Menlo;\"><span style=\"color: #888a85;\">\/\/ Sets \"newParent\" as the new parent of the child GameObject.<\/span>\r\n<span style=\"color: #222222;\">child<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">transform<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">SetParent<\/span><span style=\"color: #222222;\">(<\/span><span style=\"color: #222222;\">newParent<\/span><span style=\"color: #222222;\">);<\/span><\/span>\r\n\r\n<span style=\"color: #888a85;\">\/\/&nbsp;Same&nbsp;as&nbsp;above,&nbsp;except&nbsp;worldPositionStays&nbsp;set&nbsp;to&nbsp;false<\/span>\r\n<span style=\"color: #888a85;\">\/\/&nbsp;makes&nbsp;the&nbsp;child&nbsp;keep&nbsp;its&nbsp;local&nbsp;orientation&nbsp;rather&nbsp;than<\/span>\r\n<span style=\"color: #888a85;\">\/\/&nbsp;its&nbsp;global&nbsp;orientation.<\/span>\r\n<span style=\"color: #222222;\">child<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">transform<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">SetParent<\/span><span style=\"color: #222222;\">(<\/span><span style=\"color: #222222;\">newParent<\/span><span style=\"color: #222222;\">,<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #009695;\">false<\/span><span style=\"color: #222222;\">);<\/span>\r\n\r\n<span style=\"color: #888a85;\">\/\/&nbsp;Setting&nbsp;the&nbsp;parent&nbsp;to&nbsp;\u2018null\u2019&nbsp;unparents&nbsp;the&nbsp;GameObject<\/span>\r\n<span style=\"color: #888a85;\">\/\/&nbsp;and&nbsp;turns&nbsp;child&nbsp;into&nbsp;a&nbsp;top-level&nbsp;object&nbsp;in&nbsp;the&nbsp;hierarchy<\/span>\r\n<span style=\"color: #222222;\">child<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">transform<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">SetParent<\/span><span style=\"color: #222222;\">(<\/span><span style=\"color: #009695;\">null<\/span><span style=\"color: #222222;\">);<\/span>&nbsp;<\/pre>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\"><strong>Transform.InverseTransformPoint<\/strong><\/p>\n<div class=\"subsection\">\n<p class=\"\">Transforms&nbsp;<code class=\"varname\">position<\/code>&nbsp;from world space to local space.<\/p>\n<\/div>\n<div class=\"subsection\">\n<p class=\"\">This function is essentially the opposite of&nbsp;<a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/Transform.TransformPoint.html\">Transform.TransformPoint<\/a>, which is used to convert from local to world space.<\/p>\n<p>Note that the returned position is affected by scale. Use&nbsp;<a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/Transform.InverseTransformDirection.html\">Transform.InverseTransformDirection<\/a>&nbsp;if you are dealing with direction vectors rather than positions.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Transform.LookAt public void LookAt(Transform&nbsp;target,&nbsp;Vector3&nbsp;worldUp&nbsp;= Vector3.up); Rotates the transform so the forward vector points at \/target\/&#8217;s current position. Then it rotates the transform to point its up direction vector in the direction hinted at by the&nbsp;worldUp&nbsp;vector. If you leave out the&nbsp;worldUp&nbsp;parameter, the function will use the world y axis.&nbsp; So if I want the gameObject to &#8230; <a title=\"Transforms &#8211; Unity\" class=\"read-more\" href=\"https:\/\/lucyestela.com\/dev\/unity\/transforms\/\" aria-label=\"More on Transforms &#8211; Unity\">Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"bgseo_title":"","bgseo_description":"","bgseo_robots_index":"index","bgseo_robots_follow":"follow","footnotes":""},"categories":[58],"tags":[14,22,13],"class_list":["post-78","post","type-post","status-publish","format-standard","hentry","category-unity","tag-lookat","tag-setparent","tag-transform"],"_links":{"self":[{"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/posts\/78","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/comments?post=78"}],"version-history":[{"count":7,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/posts\/78\/revisions"}],"predecessor-version":[{"id":245,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/posts\/78\/revisions\/245"}],"wp:attachment":[{"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/media?parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/categories?post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/tags?post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}