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 John-Lisenby · May 19, 2015 at 08:53 PM · c#looting

Loot Explosion

Hello,

I want to know how can I create some kind if loot explosion. Here is the script I am using to create the item.

 using UnityEngine;
 using System.Collections;
 
 public class LootManager : MonoBehaviour
 {
     public GameObject[] lootObjects;// objects which can be dropped as loot
     public GameObject[] cashGo;
    
 
     public int chance;
 
     public void DropLoot(Vector3 dropPosition)
     {
         chance = Random.Range(1, 100); // Adds another chance to spawn an item
         if (chance > 1 && chance < 50) // 50% chance
         {
             // create a random number
             int lootNumber = Random.Range(0, lootObjects.Length);
             // create a new gameobject on the position of the enemy
             GameObject loot = (GameObject)Instantiate(lootObjects[lootNumber], dropPosition, Quaternion.EulerRotation(-90, 0, 0));
             Debug.Log("Loot obj to spawn" + lootNumber);
         }
         Debug.Log("Change to spawn" + chance);
     }
 
 
 }
 
 
 

I want the item to spawn and pop upwards and land a small distant from the NPC location. Something like in Marvel Hero’s.Video

Thanks,

John

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 Cherno · May 19, 2015 at 09:05 PM

Give the loot object a rigidbody, and use AddForce (Forcemode Impulse) that make the loot object jump in a random direction and upwards.

Comment
Add comment · Show 3 · 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 John-Lisenby · May 19, 2015 at 09:57 PM 0
Share

Hi Cherno,

Thanks for the reply. I added this code to the start function and it just pops up and back down. I am not sure where to add in the random direction part.

  private Rigidbody rb;
 
 void Start()
 {
      rb = GetComponent<Rigidbody>();
         //Vector3 = new Vector3()
         rb.AddForce(Vector3.up * 7, Force$$anonymous$$ode.Impulse);
     
 }

Thanks again,

John

avatar image Cherno · May 19, 2015 at 10:07 PM 0
Share

Imediately after you instantiate the object.

 GameObject loot = (GameObject)Instantiate(lootObjects[lootNumber], dropPosition, Quaternion.EulerRotation(-90, 0, 0));
 loot.GetComponent<RigidBody>().AddForce(Vector3.up * 7, Force$$anonymous$$ode.Impulse);
              Debug.Log("Loot obj to spawn" + lootNumber);

You can first put a reference to the rigidbody component into a seperate variable like you did, but since you only need to access it once (to add the force), I just used GetComponent directly.

avatar image John-Lisenby · May 19, 2015 at 11:03 PM 0
Share

Thanks for your help! It working.

John L

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

20 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Flip over an object (smooth transition) 3 Answers

can someone help me with my looting script? c# 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