#create an object called EnemyDrop add
1 messages · Page 1 of 1 (latest)
but what if i have multiple different enemies? I guess i could just ake a prefab for each enemy type, and the prefab woulld include itemDrop list
??
would you like the code to handle the changes for you, or would you like to make many different prefabs
code to handle the changes
right. so continue with what I mentioned
ok i made the object
in that EnemyDrop script make a variable called private MyScriptableObject dropInformation;
in the object, you will need a sprite renderer, text, whatecver you want to display
configure that first, then show me what u got
ok did this]
here
wait not a sphere colider i removed that
you alreayd have the scriptable object
so itemName should be a Text type, for it to be displayed
right?
(preferably a TMP_Text) as it's far superior to normal text
ah i made it with just a string
?
sorry, im really new to unity so
thats fine
yep, show me it
[CreateAssetMenu(menuName = "Inventory/Item Definition", fileName = "New Item Definition")]
public class ItemDefinition : ScriptableObject
{
[SerializeField] private string _itemName;
[SerializeField] private bool _isStackable;
[SerializeField] private Sprite _inGameSprite;
[SerializeField] private Sprite _uiSprite;
public string itemName => _itemName;
public bool isStackable => _isStackable;
public Sprite inGameSprite => _inGameSprite;
public Sprite uiSprite => _uiSprite;
}
ok, that's perfectly fine. leave it alone
ok
now configure it in the object inspector
drag and drop the references, show me a pic of that once done
drag in the text objects
this is for the drops only btw. for UI you'll do that yourself
How does a ScriptableObject differ from a MonoBehaviour with serialized fields?
Wait, I should just watch the tutorial. Ignore me.
Grand. now write this:
public void Initialize(ItemDefinition droppedItem)
{
dropInformation = droppedItem;
name.text = dropInformation.name;
//continue for all information;
}```
edited slightly.
I notice that you made dropInformation public
Don't do that. make it private
it should not be exposed
oh, wait so i assign nothing to it?
or serializeField?
public class EnemyLootDrop : MonoBehaviour
{
private ItemDefinition dropInformation = null;
public TMP_Text itemName;
public TMP_Text amount;
public bool isStackable;
public Sprite itemSprite;
public void Initialize(ItemDefinition droppedItem)
{
dropInformation = droppedItem;
itemName.text = dropInformation.name;
isStackable = dropInformation.isStackable;
itemSprite = dropInformation.inGameSprite;
//continue for all information;
}
}
is should maybe remove the amount
why have you changed SpriteRenderer to Sprite
the ScriptableObject holds the sprite
and we want to Display that onto the sprite renderer
right?
ahh the ItemDefinition had just the sprite
yes, as it should
and this class, should display that sprite onto a sprite renderer
mhmm
[SerializeField] private EnemyLootDrop lootPrefab;
[SerializeField] private ItemDefinition lootInformation;```
write these
also, prefab this loot object we've just made.
then plug in the references
show me once done
a small question
shoot
I don't follow
as a child of EnemyDrop?
yes
depends how you want your drops to look, it's a design question only you can answer
looks fine to me. how does it look in scene view?
cool, have you wrote the 2 lines in the slime script and dragged in the stuff
I guess the sprite renderer is hidden by the text? since I don't see it
yes
its just below the plane
done
sick
now, when enemy dies
simply just..
EnemyLootDrop drop = Instantiate(the loot prefab, pos, rot);
drop.Initialize(theInformation);
and you're done
ok
remove the amount TMP text
as thats just for your inventory I'd think.
that's a seperate thing
I would also advise putting your sprite renderer as a child
so then you can scale it independantly from the root object
oh ok
(always best to keep root objects at 1,1,1 scale)
but now do that, and scale your text properly so it's not just massive and make a new video (and move text up)
(and scale the drop sprite up abit, seems abit small to me)
ok ill do that
so just make an empty obj with sprite renderer component?
correct
ok all done
Does it work
Sik, do you have other enemy types yet? or atleast a different drop
use that potion bottle
make a new SO instance, call it "Heal potion"
yes i have another drop
and drop it into the enemy loot information of the slime
already done
ok ezpzp
here
ezpz right?
yeah
oh but what if i want slime to have both drops?
i know to make an array and loop through it i suppose
change
[SerializeField] private ItemDefinition lootInformation;
to
[SerializeField] private List<ItemDefinition> lootInformation;
//on death:
foreach(ItemDefininition lootInfo in lootInformation)
{
Vector3 dropLocationOffset = Random.onUnitSphere * 3; (3 == some arbitary radius)
dropLocationOffset.y = 0;
Vector3 dropLocation = transform.position + dropLocationOffset;
EnemyLootDrop drop = Instantiate(lootPrefab,dropLocation,...);
drop.Initialize(lootInfo);
}```
randomized drop location around death point, so they try to not double up on one another
try it with like.... 10 elements in the list. lets see what happens
yeah it works
cool, I'll tell you the next steps, but that will be your homework
Vector3 dropLocation = transform.position + dropLocationOffset; I used transform.position for the spawn location
if thats incorrect, use what you have been using
plus the offset
which is correct, as we don't want a randomized spawn height
we want it to spawn at deathLocation.y, right?
what did you use before, for instantiate?
now it works perfectly
just position
nice, right so back to homework task
EnemyLootDrop will handle the player picking up the loot, either a magnetic effect, or a simple OnTriggerEnter.
When you pick up the item, you could also add a call to an InventoryManager. to add it to your inventory.
the EnemyLootDrop already has the information of the item stored. so it should be simple enough to pass that to the inventory manager, to add the stats etc to the correct thing.
how you go about doing that, is totally upto you.
ahh right ok
ping me at somepoint in the future when you've figured it out, I'd like to see the progression
Stretch task:
-Make the sprite renderer bob up and down (Research: move Y posiition of sprite renderer object via a Mathf.Sin wave.)
-Make the text only visible if the player is within X units of the dropped item.
i suppose this will change for each enemy, so would i need to assign the loot manually?
correct
ahh ok
maybe better if i made a prefab for each enemy type?
so i can just instantiate the slimes, all will have simmilar loot, wtc
for the loot? no, you wuold still need to drag in the prefab anyway
so what does it matter
ohh
theres only so much automation you can do
dang
ok, ill bear that in mind
but still upon instantiate, would i still need to assign loot?
Can you explain better
if it is assigned beforehand in prefab, when i instantiate the enemy prefab, would i still need to assign it again?
No
this is why I asked this right at the start
yeah but i suppose i will need to make a prefab of each enemy type...
or maybe a scriptable object is better?
yeah it seems better on my case too
but they all use the same EnemyAI script
it's abit complicated
uses inheritance and ScriptableObjects for their attack data etc
for roaming and chasing? yes most likely
yeah attack data will change, and maybe their range of sight etc
here i got it to add to the inventory
Very nice
No problemo!
I want to make some pop up animation, and add gravity to the item too, so that it always falls on the ground
for popup: Please look into Dotween
(Event when i hit the enemies mid- air)
it'll change your life
i did
my slime jump animation and Hit animation are done using DOTween
Wow is this your game?
Yep, it's in my bio
looks so stylish!
Not too many games like this around
Dungeon Crawler rogue like
I'm doing one of those but 2D.
I think I have my twitter linked to my discord, feel free to follow me for updates
im gonna make something similar but with Octopath traveller graphics (2.5D)
something like this, maybe not this beautiful, but close
nice
private void ItemDrop()
{
foreach (ItemDefinition lootInfo in lootInformation)
{
Vector3 dropLocationOffset = Random.onUnitSphere * 1; //(1 == some arbitary radius)
dropLocationOffset.y = 0;
Vector3 dropLocation = transform.position + dropLocationOffset;
GameItem drop = Instantiate(lootPrefab, transform.position, Quaternion.identity);
drop.Initialize(lootInfo);
drop.transform.DOJump(
endValue: dropLocation,
jumpPower: 0.2f,
numJumps: 1,
duration: 0.5f);
}
``` here's the code, just added DOJump
if you want to clean it up, you can do all of that inside Initialize
but hell yea dude, looks rad
yeah but then the dropLacation will also need to change, etc, i just did it here for convinience, andin the future, i might wanna animate the items in a different way, depending on how they are spawned, etc.