Zelda games are well-known for their smaller interactions within the environment. Being able to cut grass, chop tress and even smash pots!
Over the last couple of days I've been trying to nail down these mechanics as best as I can. So far I've got trees working, I've got grass working to a degree (my method causes a drastic fps drop simply because I've placed over 500 grass objects as theres no simple way to go about this). And now, I've got pots working too.
All of these objects share the same mechanic, in that they are essentially "enemies". As such, I've coded a means to allow interaction between them and the player based on a simple AI code provided with the engine.
In the inspector on the right I've scripted on to the existing code to allow for item spawns! This is independant too for each object. so I can set whether or not its able to spawn anything upon death and also attach any prefab I want to spawn at said time.
My code checks when the enemy is dead, if its not allowed to respawn (pots and trees cannot), it then checks to see what type of "enemy" has been destroyed. It's not exactly clear in the code, but trees in the game have a rigidbody, which alows them to topple over, where as pots are static and dont move.
So checking which is which on death the spawns and obect set in the inspector if allowed and if its a tree will add physical force to simulate a sort of jump.
You can also see a check that tells the game to spawn an item set in the inspector if its allowed to. This would work with anything, but ideally it needs to be something that can be picked up otherwise everytime an enemy is destroyed any object instantiated would just clutter the game.
So for now, I made a few Rupee prefabs, that in turn have a check I scripted to allow for them to be either statically placed or if spawned, spring upwards like they do in the game.
By default, the bool spawn is true, so that when its instantiated via the enemy death it will automatically bounce upwards. When manually placing these all I need to do it disable spawn, and then it will behave exactly as it should in the world.
So together, these provide the basis in which objects can be interacted with in game. And is easily expandable, seeing as any prefab can be spawned.
I was dabbling in random ranges as in the games it usually is by chance when a object is spawned, but for the purposes of this project it's not entirely needed. I may add it for grass, because manually setting over 500 grass objects to spawn or not spawn anything will be very tiresome.
In the enemy script it would look something like this:
int rnd = UnityEngine.Random.Range(1, 3);
if (rnd == 3 && spawnPickup)
{
Instantiate(spawnItem, transform.position, transform.rotation);
}
This would give each object a 1 in 3 chance of spawning an item. I could go even further and have mutliple spawn items to choose from. So the code would reflect this:
int rnd = UnityEngine.Random.Range(1, 3);
if (rnd == 3 && spawnPickup)
{
int rndnew = UnityEngine.Random.Range(1, 3)
switch (rndnew)
{
case 1:
Instantiate(spawnItemA, transform.position, transform.rotation); break;
case 2:
Instantiate(spawnItemB, transform.position, transform.rotation); break;
case 3:
Instantiate(spawnItemC, transform.position, transform.rotation); break;
}
}
It might not be the best way to script it, but it would work and could be expanded to add a new pickup everytime a new one was created.
No comments:
Post a Comment