Hi guys,
I’m an artist making a demo level with some of my assets. I’ve been “programming” (following tutorials) for like two weeks now so I only know enough to make slight modifications to code. The following works, but I’d like to add functionality, which is a little beyond what I’m capable of right now… I don’t expect anyone to write anything for me, but some solid guidance would be greatly appreciated!
First, where would I stick a Random.Range in here? I’d like to enter my entire loot table into the array and have the script choose from the items.
Second, how would I tell it to only drop a specific number (or even better, a random range) of items.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ItemDropper_Death : MonoBehaviour
{
public List<GameObject> objectsToDropOnDeath;
public int offSetWidth = 10;
public void AddItemToDrop(GameObject newItem)
{
if (newItem != null)
{
if (!objectsToDropOnDeath.Contains(newItem))
{
objectsToDropOnDeath.Add(newItem);
}
}
}
public void OnDeath()
{
if(objectsToDropOnDeath.Count > 0)
DropItems();
}
private void DropItems()
{
foreach (GameObject myObject in objectsToDropOnDeath)
{
if(myObject != null)
InstantiateNewObject(myObject);
}
}
private void InstantiateNewObject(GameObject objectToDrop)
{
Instantiate(objectToDrop, GetPosition(), GetRotation());
}
private bool offsetLeft = true;
private Vector3 GetPosition()
{
Vector3 newPosition = new Vector3();
int randX = (int)Random.Range((offSetWidth / 1.5f), offSetWidth);
int negRandX = (int)Random.Range(-offSetWidth, -(offSetWidth / 1.5f));
int xOffset = 0;
if (offsetLeft)
xOffset = negRandX;
else
xOffset = randX;
offsetLeft = !offsetLeft;
newPosition = new Vector3(this.transform.position.x + xOffset, this.transform.position.y, this.transform.position.z);
return newPosition;
}
private Quaternion GetRotation()
{
Quaternion rot = new Quaternion(0.0f, 0.0f, 0.0f, 0.0f); //rotation of 0, needed for instantiation
return rot;
}
}