What i want are ‘trees’ that can be harvested for resources. To harvest player needs to be next to the item. This is the strategy i thought up. What do you think about it and how would you do it?
If tree is clicked. Check if player is close enough to the tree. If so begin harvest.
As long as the mousbutton is down, drain tree of resources and pass them to the player.
When trees resource is zero, kill the tree. Death animation followed by destruction of object.
Normal GameObjects should work fine. Unity’s buildin terrain features are a pain in the ass to work with via scripting. As it is now, those terrains are only useful for a prebuild level design and no modifcation is done in-game.
And yes, that would be a good way on how to do that. If you want to be really cautious with performance, make sure the player decides what it detects with an interaction, not every tree on its own. (Dont use the update in the tree component, as with a lot of trees you will get a lot of update checks which are unnecessary)
You sir are a lifesaver! I was about to do just that! But you make alot of sense.Thank you.
The reason is that it is easy to use the Trees OnMouseDown method. But then i would have to check for stuf in the update method of the tree anyway.
I should have nothing in the trees update and just go with letting the tree have public methods that the player or other objects calls when interacting. So then i have to figure out how to get the object the mouse is clicking on.
What you could do though in regards to the resource draining, is starting a Coroutine when you detect the interaction. You can just let the tree itself handle everything from then on, as your player script could get a bit overcrowded when you do it inside there. It’s also a good programming practice to let objects handle as much as possible on their own.
For the player interaction, you could do a Raycast from the players position and rotation for a X length. If the raycast hits something, make a SendMessage call with the name of the method you want to invoke on the tree.
Im not really understanding the Coroutine well enough. What i started to do now is using MouseEnter/Over/Down/Up and method Tree.Harvese() that is called as long as the mouse is over the tree, button is down and player is close enough. It starts from mouseOver so it can only run in one Tree at a time. How does that sound?
That should work, but I don’t know wether the OnMouseEnter etc methods cause any overhead, as it is something internal from unity. But what I mean is, instead of gathering resources from the Update function (as far as I understand, that’s what you’re doing now), you can use a coroutine. If understanding correctly, this is what you are doing now:
OK! I like it. So when the tree is cliked the while loop starts, but only for as long as the button is down and only as long as the tree has resources?
Actually im thinking further away. Im doing this in a base clase and setting up your coroutine code so that it will call a virtual method called ‘IsBeingClicked’ and then calls Virtual ‘NoLongerClicked’ when mouseExits or button no longer is pressed. And then all the classes that derives can react to the mouseclicking differenlty.