- Home /
Instantiating a random dropped consumable item from many cloned objects
My goal is to have this script drop a random consumable item (2 or 3 objects) to drop after the 5 second time limit. I can't get each cloned object to drop a random item (or one at all for that matter). The issues I run into is either the parent object will turn into an item or no 'clones' will be instantiated and an object just spawns right on the parent. and gets a tad bit glitchy because of two objects colliding on the same world space. Please help out asap if you can! :D
#pragma strict
#pragma implicit
#pragma downcast
var prefab : Rigidbody;
var speed = 15;
var numberOfObjects = 15;
var radius = 1;
function Update()
{
if(Input.GetButtonDown("Fire1"))
{
LaunchingProjectile();
}
}
function LaunchingProjectile()
{
for (i = 0; i < numberOfObjects; i++)
{
var angle = i * Mathf.PI * 2 / numberOfObjects;
var position = Vector3 (Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;
var clone : Rigidbody = Instantiate(prefab, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection( Vector3 (0, speed, 0));
clone.transform.parent = transform;
clone.transform.localPosition = position;
Destroy(clone.gameObject, 5);
}
}
$$anonymous$$usclegunner...Could you please share how you got random items to drop from cloned objects when they are destroyed as I am attempting to do this and am having a bugger of time. Thank you in advance.
Answer by AngryOldMan · Apr 26, 2011 at 07:57 PM
this line
var clone : Rigidbody = Instantiate(prefab, transform.position, transform.rotation);
instead add
var offset : Vector3 = new Vector3 (1,1,1);
var clone : Rigidbody = Instantiate(prefab, transform.position + offset, transform.rotation);
then alter the offset variable so that the prefab appears in the right position
Ok, but now how do I get a random item to drop in place of the cloned object after it's destroyed?
NV$$anonymous$$ I figured out what was wrong, but thanks for the help