Hi:
i’m making a third person shooting game, what i trying to do is the player can pick up items after the monster is killed. i used a EnemyManager to instantiate the monster prefab and a LootManager(empty gameobject with script) to control the probability for what items the monster will drop.
My problem is i found that the item appeared at the monster spawn point but not at the position when the monster died.
[92879-dsfsse324.jpg*|92879]
this is the script attached to EnemyManager
public class EenemyManager : MonoBehaviour {
public PlayerHealth playerHealth;
public GameObject enemy;
public float spawnTime = 8f;
public float waitToStart = 8f;
public Transform spawnPoint;
// Use this for initialization
void Start () {
InvokeRepeating("Spawn", waitToStart, spawnTime);
}
void Spawn()
{
if (playerHealth.currentHealth<=0)
{
return;
}
GameObject enemyclone = Instantiate(enemy, spawnPoint.position, spawnPoint.rotation) as GameObject;
}
}
this is the script attached to LootManager
public class LootScript : MonoBehaviour {
[System.Serializable]
public class DropItem
{
public string name;
public GameObject item;
public int dropRarity;
}
public List<DropItem> LootTable = new List<DropItem>();
public int dropChance;
GameObject[] enemies;
Vector3 pos;
void Start()
{
if (enemies == null)
{
enemies = GameObject.FindGameObjectsWithTag("Enemy");
foreach (GameObject enemy in enemies)
{
pos = enemy.transform.position;
}
}
else
{
return;
}
}
public void calculateLoot()
{
int cal_dropChance = Random.Range(0, 101);
if (cal_dropChance>dropChance)
{
Debug.Log("No Loot For Me");
return;
}
if (cal_dropChance<=dropChance)
{
int itemWeight = 0;
for (int i = 0; i < LootTable.Count; i++)
{
itemWeight += LootTable*.dropRarity;*
}
Debug.Log("ItemWeight " + itemWeight);
int randomValue = Random.Range(0, itemWeight);
for (int j = 0; j < LootTable.Count; j++)
{
if (randomValue<=LootTable[j].dropRarity)
{
Instantiate(LootTable[j].item, pos, Quaternion.identity);
}
randomValue -= LootTable[j].dropRarity;
Debug.Log("Random Value Decreased " + randomValue);
}
}
}
}
could anyone can tell me how to make the dropped item appear at the position of the monster die but not at the spawn point, please? thank you.
*