How can I make the game recognize when I have 60 rocks in my inventory.
Alright so in my game you are stranded on an island. Your objective is to find 60 rocks, and then return to a point on the island, once that happens I want an animation to kick in zooming out of the islands and the rocks will be arranged to say "S.O.S", but how do I do that? How do I have the game recognize that I have 60 rocks in my inventory? How do I have the game recognize when i'm in a certain spot with 60 rocks in my inventory, and when that happens have the animation kick in? Thanks. Also, the script i'm using for my inventory is Inventory Master.
Oh, and if you respond with a semi complicated answer, could you please just tell me what to google and if it should help me or not, i'm VERY new to Unity and Scripting.
i know zero knowledge of Inventory $$anonymous$$aster and no intention to change that, so the following answers may not be easily applicable ;)
"How do I have the game recognize that I have 60 rocks in my inventory?" maintain a counter of the collected rocks.
"How do I have the game recognize when i'm in a certain spot with 60 rocks in my inventory" when you get to that spot (probably using a collider of some sort), simply check the counter
"...when that happens have the animation kick in?" if you're at the location and the count is sufficient, play the animation.
if you need to learn how to do those things, there are many tutorials for colliders and animation. the counting stuff is beginners stuff and, since it sounds like you have some experience, it should be straightforward for you ;)
in future, try to ask a single question at a time... you're more likely to get specific answers. asking unclear, vague or multiple-part questions lessens the likelihood of that! check the faq for guidelines and advice on what constitutes a good question.
Answer by tanoshimi · Apr 29, 2016 at 07:54 PM
Add a Collider marked as "Is Trigger" at the spot on the island you need to return to. Then add a script with the following function attached to it.
[Tooltip("Drag your player's inventory into this slot")]
[SerializeField]
private Inventory playerInventory;
[Tooltip("Set this to the ID of the stone in your inventory system")]
[SerializeField]
private int stoneID;
// Something entered the spot...
void OnTriggerEnter(Collider other) {
// .. was it the player?
if (other.CompareTag("Player") ) {
// If so, rummage through their inventory
List<Item> items = playerInventory.getItemList();
// and count up how many stones they have
int numStones = 0;
for(int i=0; i<items.Count; i++) { if (items[i].itemID == stoneID) numStones++; }
// Do they have enough?
if(numStones >= 60) { SOSAnimation.Play(); }
}
}
Thank you so much for the help, i've been following the directions, you say "Drag your player's inventory into this slot" But where is the slot you speak of? Do I drag the script into that script somehow or should there be a slot in the script settings when I look at it in Unity. Thanks again. Also I seem to be getting errors, on lines "3,19" "7,13" "10,6." All of them are an "A namespace can only contain types and namespace declarations" Error.
This is only snippets of the script - it needs to be contained in a proper C# class definition, in which case "playerInventory" becomes a slot exposed in the inspector panel onto which you can drag and drop the player inventory.
Answer by normand · Apr 30, 2016 at 08:21 AM
weel you have allready your function if(numStones >=60){ Debug.Log("Do My Animation can play") }
But your probleme look like if you know how play the annimation. I suggest you yo put a ''''''''Debug.Log("Do My Animation can play")'''''''''''()
So, what i doe its .... Animator _animotor; bool canAnimate; void Start(){ animator = GetComponent(); } void Update(){ _animator.SetBool("CanPlay", canAnimate);
} and put your line code inside your trigger function if(numStones >=60){ Debug.Log("Do My Animation can play") canAnimate=true; }
adn make sure that your Paramater bool name in your animatorController is the same name as CanPlay in your script. and dont use the old Lagacy animation. So, set your model as Generic.
Please format your code. This is horrible to read. ;)
Your answer
