{"id":1802,"date":"2013-04-02T17:25:57","date_gmt":"2013-04-02T14:25:57","guid":{"rendered":"http:\/\/unitycoder.com\/blog\/?p=1802"},"modified":"2014-08-26T21:21:39","modified_gmt":"2014-08-26T18:21:39","slug":"inventory-script-unity","status":"publish","type":"post","link":"https:\/\/unitycoder.com\/blog\/2013\/04\/02\/inventory-script-unity\/","title":{"rendered":"Inventory Script Test"},"content":{"rendered":"<p><a title=\"http:\/\/unitycoder.com\/upload\/demos\/InventoryRealObjects1\/\" href=\"http:\/\/unitycoder.com\/upload\/demos\/InventoryRealObjects1\/\" target=\"_blank\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1808\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2013\/04\/02\/inventory-script-unity\/inventory_script_3d_objects_unity_2\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2013\/04\/inventory_script_3D_objects_unity_2.jpg?fit=680%2C553&amp;ssl=1\" data-orig-size=\"680,553\" 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=\"inventory_script_3D_objects_unity_2\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2013\/04\/inventory_script_3D_objects_unity_2.jpg?fit=300%2C243&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2013\/04\/inventory_script_3D_objects_unity_2.jpg?fit=680%2C553&amp;ssl=1\" class=\"alignnone size-full wp-image-1808\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2013\/04\/inventory_script_3D_objects_unity_2.jpg?resize=680%2C553\" alt=\"inventory_script_3D_objects_unity_2\" width=\"680\" height=\"553\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2013\/04\/inventory_script_3D_objects_unity_2.jpg?w=680&amp;ssl=1 680w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2013\/04\/inventory_script_3D_objects_unity_2.jpg?resize=300%2C243&amp;ssl=1 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/a><\/p>\n<p>Testing one idea for a inventory system. (free <a title=\"https:\/\/www.assetstore.unity3d.com\/#\/content\/876\" href=\"https:\/\/www.assetstore.unity3d.com\/#\/content\/876\" target=\"_blank\">sword from assetstore<\/a>)<\/p>\n<p>Basic idea is,<br \/>\n&#8211; Use the same objects inside the inventory (3D model, with colliders)<br \/>\n&#8211; No need to draw icons or to use unity GUI()<br \/>\n&#8211; Objects take realistic amount of space, you could even try to fit lots of objects by placing them between others (or maybe later on top of each other, just check for stack maxheight..)<br \/>\n&#8211; Maybe later could try even more realistic 3D inventory: 3D bag, objects are rigidbodies that you push\/pull inside the bag (and they fall and mix there with physics.. glass objects would break if collision magnitude is &gt; x..)<\/p>\n<p><strong>Webplayer:<\/strong><br \/>\n<a title=\"http:\/\/unitycoder.com\/upload\/demos\/InventoryRealObjects1\/\" href=\"http:\/\/unitycoder.com\/upload\/demos\/InventoryRealObjects1\/\" target=\"_blank\">http:\/\/unitycoder.com\/upload\/demos\/InventoryRealObjects1\/<\/a><\/p>\n<p>(sound from: <a title=\"http:\/\/www.bfxr.net\/\" href=\"http:\/\/www.bfxr.net\/\" target=\"_blank\">http:\/\/www.bfxr.net<\/a> )<\/p>\n<p>&#8212;<\/p>\n<p>Info:<br \/>\n&#8211; swords: tag = pickable, layer = pickables)<br \/>\n&#8211; ground: tag = ground<br \/>\n&#8211; inventory plane: tag = inventory<\/p>\n<p><strong>Source C# (Inventory.cs) :<\/strong><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\r\nusing UnityEngine;\r\nusing System.Collections;\r\n\r\n\/\/ TODO:\r\n\/\/ - add borders to inventory &quot;box&quot;\r\n\/\/ - drag from clicked pos, not from pivot (take offset onClick)\r\n\r\npublic class Inventory : MonoBehaviour {\r\n\r\n\/\/private LayerMask mask = -1;\r\npublic Transform inventoryPlane;\r\n\r\npublic dragHelper dragHelperScript;\r\n\r\nprivate bool grabbed = false;\r\nprivate Transform grabbedObj;\r\nprivate int layermask = 9; \/\/&lt;&lt; 9;\r\nprivate int layermaskInverted = 1&lt;&lt; 9;\r\nprivate Vector3 grabOffset;\r\n\r\nprivate Vector3 origPos;\r\n\r\nprivate Ray ray;\r\nprivate\u00a0\u00a0 \u00a0RaycastHit hit;\r\n\r\nprivate bool inventoryVisible=true;\r\n\r\nvoid Start ()\r\n{\r\n\r\n}\r\n\r\nvoid Update ()\r\n{\r\n\r\n\/\/ inventory on off\r\nif (Input.GetKeyDown(&quot;i&quot;))\r\n{\r\ninventoryVisible = !inventoryVisible;\r\nif (inventoryVisible)\r\n{\r\ninventoryPlane.position -= new Vector3(100,0,0);\r\n}else{\r\ninventoryPlane.position += new Vector3(100,0,0);\r\n}\r\n}\r\n\r\n\/\/ user clicks\r\nif (Input.GetMouseButtonDown(0))\r\n{\r\n\/\/ check for anything pickable\r\nray = Camera.main.ScreenPointToRay(Input.mousePosition);\r\nif (Physics.Raycast(ray, out hit, 50))\r\n{\r\n\/\/float distanceToGround = hit.distance;\r\nif (hit.transform.tag == &quot;pickable&quot;)\r\n{\r\n\/\/ grab it\r\n\/\/Debug.Log(hit.transform.name);\r\ngrabbed = true;\r\ngrabbedObj = hit.transform;\r\n\/\/grabbedObj.collider.isTrigger = true;\r\n\r\norigPos = grabbedObj.position;\r\n\r\ngrabOffset = grabbedObj.position-hit.point;\r\n\r\nPlaySound(2);\r\n\r\n\/\/ TODO: if it is inside inventory, then remove helper and rb first..\r\n\r\n\/\/ create boundingbox? to check for collisions\r\n\r\n\/\/ add dragChecker script to the object (has onColliderStay())\r\n\/\/ add rigidbody, if not exists\r\nif (!grabbedObj.rigidbody)\r\n{\r\ngrabbedObj.gameObject.AddComponent(&quot;Rigidbody&quot;);\r\n}\r\n\r\ngrabbedObj.rigidbody.useGravity = false;\r\ngrabbedObj.rigidbody.constraints = RigidbodyConstraints.FreezeAll;\r\n\r\n\/\/ add helperscript\r\n\r\n\/\/dragHelperScript = grabbedObj.gameObject.AddComponent&lt;dragHelperScript&gt;();\r\ngrabbedObj.gameObject.AddComponent(&quot;dragHelper&quot;);\r\n\r\n\/\/ reset collision\r\ngrabbedObj.gameObject.GetComponent&lt;dragHelper&gt;().isColliding = false;\r\ngrabbedObj.gameObject.renderer.material.color = Color.white;\r\n\r\n}\r\n}\r\n\r\n}\r\n\r\n\/\/ we are dragging something\r\nif (grabbed)\r\n{\r\n\r\nray = Camera.main.ScreenPointToRay(Input.mousePosition);\r\n\/\/if (Physics.Raycast(ray, out hit, 100, layermask))\r\nif (Physics.Raycast(ray, out hit, 100, layermask))\r\n{\r\n\/\/grabbedObj.position = hit.point+grabbedObj.collider.bounds.size;\r\ngrabbedObj.position = hit.point+grabOffset+Vector3.up*grabbedObj.collider.bounds.size.y;\r\n\r\n}else{\r\nDebug.Log(&quot;some error..&quot;);\r\n\/\/ drag it near camera..given distance\r\n\/\/Vector3 p = Camera.main.ScreenToWorldPoint(Input.mousePosition)+new Vector3(0,0,10);\r\n\/\/grabbedObj.position = p;\r\n}\r\n\r\n}\r\n\r\n\/\/ dropit\r\nif (Input.GetMouseButtonUp(0))\r\n{\r\n\r\nif (!grabbed) return;\r\n\r\n\/\/ todo: cannot drop if its colliding with 1 or more objects\r\nif (!grabbedObj.gameObject.GetComponent&lt;dragHelper&gt;().isColliding)\r\n{\r\n\r\nray = Camera.main.ScreenPointToRay(Input.mousePosition);\r\nif (Physics.Raycast(ray, out hit, 100, layermask)) \/\/ we hit inventory?\r\n{\r\n\r\n\/\/Debug.Log(hit.transform.name);\r\n\r\n\/\/ dropped to inventory\r\nif (hit.transform.tag == &quot;inventory&quot; &amp;&amp; grabbed)\r\n{\r\n\r\n\/\/ add rigidbody, if not exists\r\nif (grabbedObj.rigidbody)\r\n{\r\nDestroy (grabbedObj.rigidbody);\r\n\/\/\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0grabbedObj.gameObject.AddComponent(&quot;Rigidbody&quot;);\r\n}\r\n\r\n\/\/\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0grabbedObj.rigidbody.useGravity = true;\r\n\/\/\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0grabbedObj.rigidbody.constraints = RigidbodyConstraints.FreezeAll;\r\n\/\/grabbedObj.rigidbody.isKinematic = true;\r\n\r\ngrabbedObj.parent = inventoryPlane;\r\n\r\n\/\/ reset collision (reset from other also?)\r\n\/\/\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0grabbedObj.gameObject.GetComponent&lt;dragHelper&gt;().isColliding = false;\r\n\/\/\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0grabbedObj.gameObject.renderer.material.color = Color.white;\r\n\r\n\/\/Destroy (grabbedObj.gameObject.GetComponent&lt;dragHelper&gt;());\r\n\r\n\/\/grabbedObj.collider.isTrigger = false;\r\ngrabbed = false;\r\ngrabbedObj = null;\r\n\r\nPlaySound(1);\r\n\r\n}\r\n\r\n\/\/ drop to ground\r\nif (hit.transform.tag == &quot;ground&quot; &amp;&amp; grabbed)\r\n{\r\nDebug.Log(&quot;1&quot;);\r\norigPos = hit.point+Vector3.up*grabbedObj.collider.bounds.size.y;\r\ndropBack();\r\n}\r\n\r\n}else{ \/\/ not hitting good object\r\n\/\/ cannot drop here\r\ndropBack();\r\n}\r\n}else{\r\n\/\/\u00a0\u00a0 \u00a0Debug.Log(2);\r\ndropBack();\r\n}\r\n}\r\n\r\n}\r\n\r\nvoid dropBack()\r\n{\r\n\/\/ remove from inventory (if it was there)\r\ngrabbedObj.parent = null;\r\n\r\n\/\/ restoro position, drag failed..\r\ngrabbedObj.position = origPos;\r\n\r\ngrabbedObj.gameObject.GetComponent&lt;dragHelper&gt;().isColliding = false;\r\ngrabbedObj.gameObject.renderer.material.color = Color.white;\r\n\r\n\/\/ remove rb\r\nDestroy (grabbedObj.rigidbody);\r\n\r\ngrabbed = false;\r\ngrabbedObj = null;\r\n\r\nPlaySound(1);\r\n\r\n\/\/ restore colors?\r\n\r\n\/\/ remove helper script\r\n\r\n}\r\n\r\nvoid PlaySound(int type)\r\n{\r\n\/\/ TODO: different types..ring, bottle, metal..\r\n\r\naudio.pitch = type;\r\naudio.Play();\r\n\r\n}\r\n\r\n}\r\n\r\n<\/pre>\n<p>dragHelper.cs<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing UnityEngine;\r\nusing System.Collections;\r\n\r\npublic class dragHelper : MonoBehaviour {\r\n\r\n\u00a0\u00a0 \u00a0public bool isColliding=false;\r\n\u00a0\u00a0 \u00a0public Transform other;\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0void OnCollisionEnter(Collision collisionInfo) \r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/ fx\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/ set color\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0if (collisionInfo.transform.tag != &quot;inventory&quot; &amp;&amp; collisionInfo.transform.tag != &quot;ground&quot;)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0{\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0isColliding = true;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0other = collisionInfo.transform;\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0renderer.material.color = Color.red;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0void OnCollisionStay(Collision collisionInfo) \r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/if (collisionInfo.transform.tag != &quot;inventory&quot;)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/Debug.Log(&quot;we are hittingh..&quot;);\r\n\/\/\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0other = collisionInfo.transform;\r\n\/\/\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0isColliding = true;\r\n\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0 \u00a0void OnCollisionExit(Collision collisionInfo) \r\n\u00a0\u00a0 \u00a0{\r\n\/\/\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(&quot;No longer in contact with &quot; + collisionInfo.transform.name);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0if (collisionInfo.transform.tag != &quot;inventory&quot; &amp;&amp; collisionInfo.transform.tag != &quot;ground&quot;)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0renderer.material.color = Color.white;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0isColliding = false;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0other = collisionInfo.transform;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\/\/ TODO: use some other way..?\r\n\u00a0\u00a0 \u00a0void Update()\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0if (rigidbody)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0rigidbody.WakeUp();\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\r\n}\r\n\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Testing one idea for a inventory system. (free sword from assetstore) Basic idea is, &#8211; Use the same objects inside the inventory (3D model, with colliders) &#8211; No need to draw icons or to use unity GUI() &#8211; Objects take realistic amount of space, you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1808,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_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":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,3],"tags":[62,464,465,462,463,268],"class_list":["post-1802","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-demos","category-unity3d","tag-3d","tag-drag","tag-drop","tag-inventory","tag-objects","tag-system"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2013\/04\/inventory_script_3D_objects_unity_2.jpg?fit=680%2C553&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/p1KTaT-t4","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/1802","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=1802"}],"version-history":[{"count":11,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/1802\/revisions"}],"predecessor-version":[{"id":3204,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/1802\/revisions\/3204"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media\/1808"}],"wp:attachment":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media?parent=1802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/categories?post=1802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/tags?post=1802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}