Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by stinafina · Oct 19, 2012 at 12:55 PM · guitexturedraginventorydrop

How to make a drag and drop inventory?

I have some experience in unity and flash and I have made a few games and now I´m trying to make a game similar to Plants vs Zombies, just in 3D. I have made inventory before, but ive never made drag and drop inventory.

what I need is basically inventory just like the one in Plants vs Zombies, where you have an inventory and when you press one button the plant follows the mouse (in my case its a 3D object), and on mouse exit the object falls on the terrain exactly where the player dropped it.

so if anyone could please help me or show me a link that shows how to do this I would be very thankful!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by stinafina · Oct 30, 2012 at 04:30 PM

thank you paulaceccon, but I actually found the answer yesterday and I figured since I know there are always lot of people asking this question and the answers are so different and a lot of times not at all what you could be looking for, I'm going to put the code that I found here.

here is the source: http://unitycoder.com/blog/2012/01/08/object-placement-pickup-with-mouse/

but that one doesnt have a inventory.. so here is the code that I fixed just a little to make it a DRAG AND DROP INVENTORY:

 // mouseplacement & pickup - v1.0 - 08.01.2012 - mgear - http://unitycoder.com/blog/
 // instantiate objects with mousebuttondown, while button is held down, move the object at mouse cursor
 // also you can pick previously added object and move it around (button held down)
 
 #pragma strict
 
 public var ObjectToPlace:Transform; // prefab to instantiate
 private var clone:Transform; // hold instantiated object on this variable
 private var inventoryOver : boolean = false; // so that you won't make an object everytime you click the mouse
 
 // mainloop
 function Update () 
 {
 
     if(Input.GetButtonDown("Fire1") && inventoryOver == true)
     {
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition); 
         var hit : RaycastHit;
         var layerMask = 1 << 8; // check help page "layerMask", here we only cast ray on layer 8 (prefab layer)
         
         
             layerMask = ~layerMask; // invert layermask, so we dont hit layer 8 (which is for prefabs)
             if (Physics.Raycast (ray, hit, Mathf.Infinity, layerMask)) 
                 clone = Instantiate(ObjectToPlace, hit.point, Quaternion.identity); 
         
     }
     
     
     
     
     if(Input.GetButtonUp("Fire1") && inventoryOver == true) // button released
     {
         clone=null; // clear clone variable
         inventoryOver = false;
     }
     
     
 
     if(Input.GetButton("Fire1") && inventoryOver == true) // mousebutton is held down
     {
         if (clone!=null) // if there is some object in clone variable
         {
             var ray2 = Camera.main.ScreenPointToRay (Input.mousePosition); 
             var hit2 : RaycastHit; 
             var layerMask2 = 1 << 8;
             layerMask2 = ~layerMask2; // we are casting rays on other layers, except 8
             if (Physics.Raycast (ray2, hit2, Mathf.Infinity, layerMask2))  // we hit something
                 {
                     clone.position = hit2.point; // move our object there
                 }
         }
     }
 
 
 }
 
 function OnMouseEnter () {
     inventoryOver = true;
 }


you put this script on the inventory, witch is just a plane (dont use GuiTexture) and connect a object to the "object to place".

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Drag and Drop GUITexture iOS IPad 1 Answer

Drag Drop Inventory With GUI Buttons. 0 Answers

Android touch and drag GUItexture problem 0 Answers

how to active a button after drag? 2 Answers

Is GUITexture slow on iOS, and would it be faster to use a plane and a separate camera? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges