function OnTriggerEnter(theCollision: Collider)
{
if(theCollision.gameObject.name=="Capsule")
{
Inventory.inventory[0]++;
Debug.Log("Picked up");
}
}
Im having trouble with my player script at the moment, I have a spotlight attached to my player that acts as the torch, enemies get repelled and destroyed by the torch as they enter it but in order to do this i needed to attach a sphere collider to my spotlight.
As i have it, both my player and my spotlight have sphere colliders. The problem is, i have the basic script above which adds one to my first inventory slot every time the player collides with the object. This works fine when my player collides with the object, the only thing is, it also adds one to my inventory slot when my torch collides with the object.
Does anyone know why this may be the case? Appreciate any responses.
Unity makes no differentiation between an object colliding with a trigger and a trigger colliding with an object…hope that makes sense. You can achieve what you want two different ways:
The long way: Have a pickup item and a use item. Colliding with the pickup item would add the use item into the inventory which could be instantiated and have different collision handling on it.
or
The short way: Add a bool to your flashlight object that checks to see if it is owned by the player before it does it’s collision check. When the item is picked up you set the bool to true and it can avoid the player collision in the future.
Here is an example of that:
var isOwned : boolean = false;
function OnTriggerEnter(theCollision: Collider)
{
if(isOwned)
return;
if(theCollision.gameObject.name=="Capsule")
{
Inventory.inventory[0]++;
isOwned = true;
Debug.Log("Picked up");
}
}
Either works, it is up to you how best to handle it in regards to your inventory management.