- Home /
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
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.
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
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.
Your answer
Follow this Question
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