#create an object called EnemyDrop add

1 messages · Page 1 of 1 (latest)

violet magnet
#

we can thread it

dark maple
#

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

violet magnet
#

you don't need to

#

but you can if you want to

dark maple
#

??

violet magnet
#

would you like the code to handle the changes for you, or would you like to make many different prefabs

dark maple
#

code to handle the changes

violet magnet
#

right. so continue with what I mentioned

dark maple
#

ok i made the object

violet magnet
#

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

dark maple
#

wait not a sphere colider i removed that

violet magnet
#

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

dark maple
#

ah i made it with just a string

violet magnet
#

?

dark maple
#

the scriptable

#

ill change it

violet magnet
#

no no

#

show me the scriptable object

#

lets go back to ground zero

dark maple
#

sorry, im really new to unity so

violet magnet
#

thats fine

dark maple
#

so

#

i made a scriptable

violet magnet
#

yep, show me it

dark maple
#
 [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;


    }
violet magnet
#

ok, that's perfectly fine. leave it alone

dark maple
#

ok

violet magnet
#

should these not be TMP_Text types?

#

to show the item name etc

dark maple
#

ahh ok yes

#

done that

violet magnet
#

now configure it in the object inspector

#

drag and drop the references, show me a pic of that once done

dark maple
violet magnet
#

drag in the text objects

#

this is for the drops only btw. for UI you'll do that yourself

split prism
#

How does a ScriptableObject differ from a MonoBehaviour with serialized fields?

#

Wait, I should just watch the tutorial. Ignore me.

dark maple
violet magnet
#

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

dark maple
#

or serializeField?

violet magnet
#

look in the function I made

#

it gets assigned via code

#

so keep it private, null

dark maple
#

okk

#

done

violet magnet
#

you wrote all information in the init function?

#

show me

dark maple
# violet magnet show me
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

violet magnet
#

why have you changed SpriteRenderer to Sprite

#

the ScriptableObject holds the sprite

#

and we want to Display that onto the sprite renderer

#

right?

dark maple
#

ahh the ItemDefinition had just the sprite

violet magnet
#

yes, as it should

#

and this class, should display that sprite onto a sprite renderer

dark maple
#

fixed it

#

itemSprite.sprite = dropInformation.inGameSprite;

violet magnet
#

ok, we're nearly done

#

now, go to your Slime enemy script

dark maple
#

mhmm

violet magnet
#
[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

dark maple
#

a small question

violet magnet
#

shoot

dark maple
#

where should the TMP text object be?

#

in the hierachy i mean

violet magnet
#

I don't follow

dark maple
#

as a child of EnemyDrop?

violet magnet
#

yes

#

depends how you want your drops to look, it's a design question only you can answer

dark maple
violet magnet
#

looks fine to me. how does it look in scene view?

dark maple
#

hmmm i cant acually see it

#

maybe i need a canvas for it?

violet magnet
#

if its UI text, you wont be able to

#

no, make a 3D text object

dark maple
#

ooohhh

#

ok

#

made the prefab

#

ill adjust the size of text later lol

violet magnet
#

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

dark maple
#

yes

dark maple
violet magnet
#

sick

#

now, when enemy dies

#

simply just..

EnemyLootDrop drop = Instantiate(the loot prefab, pos, rot);
drop.Initialize(theInformation);

#

and you're done

dark maple
#

okk

#

i wrote it

violet magnet
#

now kill a slime

#

and show me a picture of what happens

dark maple
#

seems like it is working

#

wow

violet magnet
#

you can remove the one you have justi nthe scene

#

since you've prefabbed it now

dark maple
#

ok

violet magnet
#

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

dark maple
#

oh ok

violet magnet
#

(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)

dark maple
violet magnet
#

correct

dark maple
#

ok all done

violet magnet
#

Does it work

dark maple
#

yep

violet magnet
#

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"

dark maple
#

yes i have another drop

violet magnet
#

and drop it into the enemy loot information of the slime

dark maple
violet magnet
#

ok ezpzp

violet magnet
#

ezpz right?

dark maple
#

yeah

#

oh but what if i want slime to have both drops?

#

i know to make an array and loop through it i suppose

violet magnet
#

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

dark maple
#

oh god

#

chaos will happen

violet magnet
#

perhaps

#

but it'd be good to see

dark maple
#

yeah it works

violet magnet
#

cool, I'll tell you the next steps, but that will be your homework

dark maple
#

but ait

#

they are all under the plane

#

not on lvl with the slime

violet magnet
#

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

dark maple
#

i guess easy fix: dropLocationOffset.y = 0

#

so 0 is added to the y value

violet magnet
#

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?

dark maple
#

now it works perfectly

dark maple
violet magnet
#

cool, though 3 is abit much it looks like hehe

#

try like 1

dark maple
#

yeah ill adjust it now haha

#

much better haha

violet magnet
#

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.

dark maple
#

ahh right ok

violet magnet
#

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.

dark maple
#

i suppose this will change for each enemy, so would i need to assign the loot manually?

dark maple
#

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

violet magnet
#

for the loot? no, you wuold still need to drag in the prefab anyway

#

so what does it matter

dark maple
#

ohh

violet magnet
#

theres only so much automation you can do

dark maple
#

dang

#

ok, ill bear that in mind

#

but still upon instantiate, would i still need to assign loot?

violet magnet
#

Can you explain better

dark maple
#

if it is assigned beforehand in prefab, when i instantiate the enemy prefab, would i still need to assign it again?

violet magnet
#

No

dark maple
#

the loot

#

ohh ok thats good then

violet magnet
#

this is why I asked this right at the start

dark maple
#

yeah but i suppose i will need to make a prefab of each enemy type...

#

or maybe a scriptable object is better?

violet magnet
#

thats how I do it in my game

#

each enemy is its own prefab

dark maple
#

yeah it seems better on my case too

violet magnet
#

but they all use the same EnemyAI script

#

it's abit complicated

#

uses inheritance and ScriptableObjects for their attack data etc

dark maple
dark maple
dark maple
violet magnet
#

Very nice

dark maple
#

yesss sir

#

thank you for the help by the way

#

very much appreciated 😄

violet magnet
#

No problemo!

dark maple
#

I want to make some pop up animation, and add gravity to the item too, so that it always falls on the ground

violet magnet
#

for popup: Please look into Dotween

dark maple
#

(Event when i hit the enemies mid- air)

violet magnet
#

it'll change your life

dark maple
dark maple
#

my slime jump animation and Hit animation are done using DOTween

dark maple
violet magnet
#

Yep, it's in my bio

dark maple
#

looks so stylish!

violet magnet
#

Not too many games like this around

dark maple
#

is it a hack n slash?

violet magnet
#

Dungeon Crawler rogue like

dark maple
#

🤩

#

my favourite genres

split prism
#

I'm doing one of those but 2D.

violet magnet
#

I think I have my twitter linked to my discord, feel free to follow me for updates

dark maple
#

im gonna make something similar but with Octopath traveller graphics (2.5D)

#

something like this, maybe not this beautiful, but close

dark maple
dark maple
#

update

#

Got the items to jump when they spawn

dark maple
# violet magnet for popup: Please look into Dotween
    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
violet magnet
#

if you want to clean it up, you can do all of that inside Initialize

#

but hell yea dude, looks rad

dark maple