Adding random drops to my TreeChop script

i’ve made a script used for chopping down tree’s that will spawn certain items when the tree has been destroyed… however i’m struggling quite a bit to get the amount of items dropped to be random and i can’t quite figure out what my next move would be.

#pragma strict

var treeHealth : int = 5;

var dropRate : float = 0.25; //25% chance of dropping

var logs : Transform;
var coconut : Transform;
var tree : GameObject;

var speed : int = 8;

function Start()
{
	tree = this.gameObject;
	rigidbody.isKinematic = true;
}

function Update()
{
	if(treeHealth <= 0)
	{
		rigidbody.isKinematic = false;
		rigidbody.AddForce(transform.forward * speed);
		DestroyTree();
	}
}

function DestroyTree()
{
	yield WaitForSeconds(7);
	Destroy(tree);
	
	if(Random.Range(0f,1f) <= dropRate)
	
	var position : Vector3 = Vector3(Random.Range(-1.0, 1.0), 0, Random.Range(-1.0, 1.0));
	Instantiate(logs, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity);
	Instantiate(logs, tree.transform.position + Vector3(2,2,0) + position, Quaternion.identity);
	Instantiate(logs, tree.transform.position + Vector3(5,5,0) + position, Quaternion.identity);
	
	Instantiate(coconut, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity);
	Instantiate(coconut, tree.transform.position + Vector3(2,2,0) + position, Quaternion.identity);
	Instantiate(coconut, tree.transform.position + Vector3(5,5,0) + position, Quaternion.identity);

by removing the
if(Random.Range(0f,1f) <=dropRate
i get 3 of each item every time however to intention was to have a 25% chance for each to spawn. anyone has any thoughts on how to make this happen?

You missed to enclose the code inside brackets after the “if (Random…)” block, so what’s happening is that the “if” statement is run only for the line 36 (where you set the variable “position”) but after it it’s still running all the other lines of Instantiate.

I suppose you meant to do:

if(Random.Range(0f,1f) <= dropRate) {
     
     var position : Vector3 = Vector3(Random.Range(-1.0, 1.0), 0, Random.Range(-1.0, 1.0));
     Instantiate(logs, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity);
     Instantiate(logs, tree.transform.position + Vector3(2,2,0) + position, Quaternion.identity);
     Instantiate(logs, tree.transform.position + Vector3(5,5,0) + position, Quaternion.identity);
     
     Instantiate(coconut, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity);
     Instantiate(coconut, tree.transform.position + Vector3(2,2,0) + position, Quaternion.identity);
     Instantiate(coconut, tree.transform.position + Vector3(5,5,0) + position, Quaternion.identity);

}

You should use a for loop for this, how you actually use the for loop could vary, in the example below I use the fact that multiplying two probabilities is the probability of both events happening. Hence a random number being less than 0.25 twice, is the same as a random number being less than 0.25 squared (0.25*0.25). So in each loop I simply multiply by my probability and check if the random number is still less!

var treeHealth : int = 5;
var dropRate : float = 0.25; //25% chance of dropping
var logs : Transform;
var coconut : Transform;

var maxDrops : int = 3;

var tree : GameObject;
var speed : int = 8;

function Start(){
	tree = this.gameObject;
	rigidbody.isKinematic = true;
}

function Update(){
	if(treeHealth <= 0){
		rigidbody.isKinematic = false;
		rigidbody.AddForce(transform.forward * speed);
		DestroyTree();
	}
}

function DestroyTree(){
	yield WaitForSeconds(7);

	SpawnDrop(logs, dropRate, 3);
	SpawnDrop(coconut, dropRate, 3);

	Destroy(tree);
}

function SpawnDrop(t : Transform, chance : float, iMax : int){
	var this_chance : float = Mathf.Clamp(chance, 0, 1);
	var rand = Random.Range(0f,1f);
	var position : Vector3 = Vector3(Random.Range(-1.0, 1.0), 0, Random.Range(-1.0, 1.0));
	for(var i : int = 0; i < iMax; i++){
		if(rand < this_chance){
			Instantiate(t, tree.transform.position + Vector3(1*2,i*2,0) + position, Quaternion.identity);
			this_chance *= chance;
		}else{
			break;
		}
	}
}

EDIT:
I realised what I had done earlier wasn’t actually correct which doesn’t bode well for my january maths exams ^^

SpawnDrop should be:

function SpawnDrop(t : Transform, chance : float, iMax : int){
	chance = Mathf.Clamp(chance, 0, 1);
	var position : Vector3 = Vector3(Random.Range(-1.0, 1.0), 0, Random.Range(-1.0, 1.0));
	for(var i : int = 0; i < iMax; i++){
		var rand = Random.Range(0f,1f);
		if(rand < chance){
			Instantiate(t, tree.transform.position + Vector3(1*2,i*2,0) + position, Quaternion.identity);
		}else{
			continue;
		}
	}
}

Merry Christmas/Happy Holidays!

Scribe