{"id":418,"date":"2012-01-08T19:25:30","date_gmt":"2012-01-08T16:25:30","guid":{"rendered":"http:\/\/unitycoder.com\/blog\/?p=418"},"modified":"2013-08-15T21:58:57","modified_gmt":"2013-08-15T18:58:57","slug":"object-placement-pickup-with-mouse","status":"publish","type":"post","link":"https:\/\/unitycoder.com\/blog\/2012\/01\/08\/object-placement-pickup-with-mouse\/","title":{"rendered":"Object placement &#038; pickup with mouse"},"content":{"rendered":"<p><a href=\"http:\/\/unitycoder.com\/upload\/demos\/mMousePlacementPick1\/\" target=\"_blank\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"419\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2012\/01\/08\/object-placement-pickup-with-mouse\/mmouseplacement1\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/mMouseplacement1.jpg?fit=680%2C433&amp;ssl=1\" data-orig-size=\"680,433\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"mMouseplacement1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/mMouseplacement1.jpg?fit=680%2C433&amp;ssl=1\" class=\"alignnone size-full wp-image-419\" title=\"mMouseplacement1\" alt=\"\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/mMouseplacement1.jpg?resize=680%2C433\" width=\"680\" height=\"433\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/mMouseplacement1.jpg?w=680&amp;ssl=1 680w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/mMouseplacement1.jpg?resize=300%2C191&amp;ssl=1 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/a><\/p>\n<p>Testing.. related to this topic: <a href=\"http:\/\/forum.unity3d.com\/threads\/118581-Placing-object-on-mouse-down\" target=\"_blank\">http:\/\/forum.unity3d.com\/threads\/118581-Placing-object-on-mouse-down<\/a><\/p>\n<p><strong>Controls<\/strong>:<\/p>\n<ul>\n<li>Left mouse button, place object<\/li>\n<li>While placing object, hold mousebutton down to keep moving the object<\/li>\n<li>With left mouse button, you can pick previously added objects (hold button down to move them)<\/li>\n<\/ul>\n<p><strong>Webplayer: (flash)<\/strong><br \/>\n<a href=\"http:\/\/unitycoder.com\/upload\/demos\/mMousePlacementPick1\/\" target=\"_blank\">http:\/\/unitycoder.com\/upload\/demos\/mMousePlacementPick1\/<\/a><\/p>\n<p><strong>Download example scene: (Unity3.5)<\/strong><br \/>\n<a title=\"Download Source\" href=\"http:\/\/unitycoder.com\/download\/index2.php?link=64&amp;uid=un1t3c0d5r\" target=\"_blank\">mMousePlacement1.unitypackage<\/a><\/p>\n<p><strong>Source:<\/strong> (messy js)<\/p>\n<p>Attach this to camera, add some prefab.<br \/>\nNote. the prefab needs to be on layer #8<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\r\n\/\/ mouseplacement &amp; pickup - v1.0 - 08.01.2012 - mgear - http:\/\/unitycoder.com\/blog\/\r\n\/\/ instantiate objects with mousebuttondown, while button is held down, move the object at mouse cursor\r\n\/\/ also you can pick previously added object and move it around (button held down)\r\n\r\n#pragma strict\r\n\r\npublic var ObjectToPlace:Transform; \/\/ prefab to instantiate\r\nprivate var clone:Transform; \/\/ hold instantiated object on this variable\r\n\r\n\/\/ mainloop\r\nfunction Update ()\r\n{\r\n\r\nif(Input.GetButtonDown(&quot;Fire1&quot;))\r\n{\r\nvar ray = Camera.main.ScreenPointToRay (Input.mousePosition);\r\nvar hit : RaycastHit;\r\nvar layerMask = 1 &lt;&lt; 8; \/\/ check help page &quot;layerMask&quot;, here we only cast ray on layer 8 (prefab layer)\r\n\r\n\/\/ first we check if we hit previously added prefab\r\nif (Physics.Raycast (ray, hit, Mathf.Infinity, layerMask))\r\n{\r\nclone = hit.transform; \/\/ grab the object that we hit, so we can move it around\r\n\r\n}else{ \/\/ we didnt hit prefab, check if are will hit something else\r\n\r\nlayerMask = ~layerMask; \/\/ invert layermask, so we dont hit layer 8 (which is for prefabs)\r\nif (Physics.Raycast (ray, hit, Mathf.Infinity, layerMask))\r\nclone = Instantiate(ObjectToPlace, hit.point, Quaternion.identity);\r\n}\r\n}\r\n\r\nif(Input.GetButtonUp(&quot;Fire1&quot;)) \/\/ button released\r\n{\r\nclone=null; \/\/ clear clone variable\r\n}\r\n\r\nif(Input.GetButton(&quot;Fire1&quot;)) \/\/ mousebutton is held down\r\n{\r\nif (clone!=null) \/\/ if there is some object in clone variable\r\n{\r\nvar ray2 = Camera.main.ScreenPointToRay (Input.mousePosition);\r\nvar hit2 : RaycastHit;\r\nvar layerMask2 = 1 &lt;&lt; 8;\r\nlayerMask2 = ~layerMask2; \/\/ we are casting rays on other layers, except 8\r\nif (Physics.Raycast (ray2, hit2, Mathf.Infinity, layerMask2))\u00a0 \/\/ we hit something\r\n{\r\nclone.position = hit2.point; \/\/ move our object there\r\n}\r\n}\r\n}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Testing.. related to this topic: http:\/\/forum.unity3d.com\/threads\/118581-Placing-object-on-mouse-down Controls: Left mouse button, place object While placing object, hold mousebutton down to keep moving the object With left mouse button, you can pick previously added objects (hold button down to move them) Webplayer: (flash) http:\/\/unitycoder.com\/upload\/demos\/mMousePlacementPick1\/ Download example scene: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":419,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[4],"tags":[108,87,107,109],"class_list":["post-418","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-demos","tag-instantiate","tag-mouse","tag-pickup","tag-place"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/mMouseplacement1.jpg?fit=680%2C433&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/p1KTaT-6K","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/comments?post=418"}],"version-history":[{"count":6,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/418\/revisions"}],"predecessor-version":[{"id":2294,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/418\/revisions\/2294"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media\/419"}],"wp:attachment":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media?parent=418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/categories?post=418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/tags?post=418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}