{"id":84,"date":"2020-06-03T03:30:34","date_gmt":"2020-06-03T03:30:34","guid":{"rendered":"https:\/\/lucyestela.com\/dev\/?p=84"},"modified":"2021-03-02T03:01:21","modified_gmt":"2021-03-02T03:01:21","slug":"mouse-position","status":"publish","type":"post","link":"https:\/\/lucyestela.com\/dev\/unity\/mouse-position\/","title":{"rendered":"Mouse Position &#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 class=\"\">Code to get the Mouse Position in a 2D app<\/p>\n<pre class=\"\"><span style=\"font-family: Menlo;\"><span style=\"color: #3363a4;\">Vector3<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">mPos<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">=<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #3363a4;\">Input<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">mousePosition<\/span><span style=\"color: #222222;\">;<\/span>\r\n<span style=\"color: #222222;\">mPos<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">z<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">=<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">-<\/span><span style=\"color: #3363a4;\">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>\r\n<span style=\"color: #3363a4;\">Vector3<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">targetPos<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">=<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #3363a4;\">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;\">mPos<\/span><span style=\"color: #222222;\">);<\/span><\/span><\/pre>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\">Then to make something face towards it.<\/p>\n<pre class=\"\"><span style=\"font-family: Menlo;\"><span style=\"color: #222222;\">yourGameObject<\/span><span style=\"color: #222222;\">.<\/span><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;\">targetPos<\/span><span style=\"color: #222222;\">);<\/span><\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Code to get the Mouse Position in a 2D app Vector3&nbsp;mPos&nbsp;=&nbsp;Input.mousePosition; mPos.z&nbsp;=&nbsp;-Camera.main.transform.position.z; Vector3&nbsp;targetPos&nbsp;=&nbsp;Camera.main.ScreenToWorldPoint(mPos); &nbsp; Then to make something face towards it. yourGameObject.transform.LookAt(targetPos);<\/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":[21,19,18,20,4],"class_list":["post-84","post","type-post","status-publish","format-standard","hentry","category-unity","tag-camera","tag-mouse","tag-position","tag-screentoworldpoint","tag-unity"],"_links":{"self":[{"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/posts\/84","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=84"}],"version-history":[{"count":4,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/posts\/84\/revisions"}],"predecessor-version":[{"id":243,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/posts\/84\/revisions\/243"}],"wp:attachment":[{"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/media?parent=84"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/categories?post=84"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/tags?post=84"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}