• 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 bponeal · Apr 19, 2018 at 04:00 AM · game development

Npc Dropping Items on death,NPC item drop on death

Hello i'm very new to Game-Dev. I currently have a NPC with a script that allows me to place 3 prefab objects into it. At the moment when the NPC is killed he drops all 3 items and they appear below the map terrain. How can i make then spawn above so the player can see them, and pick them up? - Also instead of dropping all 3 items maybe choose at random or have % chance per item ?

 public GameObject[] items = new GameObject[3];
 
 if (CurrentHealth <= 0)
         {
             Die();
 
             Vector3 position = transform.position;
             foreach (GameObject item in items)
             {
                 if (item != null)
                 {
                     
                     Instantiate(item, position, Quaternion.identity);
                 }
 
             }
         }


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
1
Best Answer

Answer by Kciwsolb · Apr 20, 2018 at 02:50 AM

Looks like you didn't post your whole code here. So I am not sure what the Die() function does. But anyways, when you Instantiate your dropped items, add some value to increase the y position of them. This should bring them above the ground (assuming we are talking about a 3D game). Something like this:

 Instantiate(item, position + new Vector3(0.0f, someValue, 0.0f), Quaternion.identity);

As far as randomizing which one(s) to drop. Pick a random number (Read here about Unity's very nice Random.Range) between 0 and how many items you have in your array (Read here about Array.Length). Then get the index of that random number from your array and instantiate it. Do this however many times you want to drop as many random items as you want. Hope this helps! :)

Unity Random.Range: https://docs.unity3d.com/ScriptReference/Random.Range.html

C# Array.Length: https://msdn.microsoft.com/en-us/library/system.array.length(v=vs.110).aspx

Comment
Add comment · Show 2 · 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
avatar image bponeal · Apr 20, 2018 at 04:29 AM 0
Share

Thank you this defiantly helped getting the items to appear above the terrain! I was wondering if it has something to do with my prefabs that is causing them to stack on top of each other ex: https://gyazo.com/0e29d270d8b33ca7fa9d5e134e6245a6 Currently working on the random item to drop code.

note: ( I went thru and reset all prefabs transforms to 0 ) still hasn't fixed the issue.

avatar image Kciwsolb bponeal · Apr 20, 2018 at 04:43 AM 0
Share

^ This should probably be a comment to my answer, as I do not believe your intention was to write an answer to your own post. In the future, use the "Add comment" button below an answer to respond or ask for further help from someone.

Anyways, as far as them overlapping, that is to be expected. There is nothing in the code to prevent that. If you wanted to prevent that, you would need to add on offset to the x and z values when you instantiate. These values would need to be different for each item you instantiate. I only changed the y value so they were moved above the ground. $$anonymous$$aybe use a couple random numbers in a similar way to randomly spread out the items. If my answer above helped, please mark it as your accepted answer. Good luck with your project! :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

77 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

As a game developer is it better to use Directx or OpenGL? 0 Answers

Unity 2d game 1 Answer

increase scale to object and it make it slowly 0 Answers

how to move a puzzle piece from current position to empty position 0 Answers

Cannot Send Class Object From One Scene to Another 1 Answer


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