1
\$\begingroup\$

how can i make items drop infront of the enemy without getting hidden under the enemy sprite due to the size of different enemies. I would also like to spread the drop items

How currently it is enter image description here

How i would like it to be enter image description here

My code:

public void TakeDamage(int damage)
{
    int dist = 10;
    Vector3 spawnPos = transform.position + transform.forward * dist;
    GroundItem.Create(new Item(3, 1), spawnPos);

    health -= damage
}

public class GroundItem : MonoBehaviour
{
    [SerializeField]
    private Item item;
    [SerializeField]
    private SpriteRenderer spriteRenderer = null;

    public static GroundItem Create(Item item, Vector3 position)
    {
        GameObject groundItemGO = Instantiate(GameManager.Instance.GetData.GetAssetDatabase.GetPrefabByName("GroundItemPrefab"), position, Quaternion.identity);
        GroundItem groundItem = groundItemGO.GetComponent<GroundItem>();
        groundItem.SetItem(item);
        return groundItem;
    }

    private void SetItem(Item item)
    {
        this.item = item;
        spriteRenderer.sprite = item.data.icon;
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ Could you not just make dist larger? \$\endgroup\$
    – Jay
    May 5, 2020 at 10:59
  • \$\begingroup\$ Yes, hur then i got the problem being items to far away from smaller creatures \$\endgroup\$
    – Printer
    May 5, 2020 at 11:01
  • \$\begingroup\$ Answer edited to include @DMGregory's suggested fix. Let me know if it workes \$\endgroup\$
    – Jay
    May 8, 2020 at 14:14

1 Answer 1

1
\$\begingroup\$

You can use Sprite bounds to get the rough size of your sprite.
In your enemy class,

float spriteSize; 

void Start(){
    Sprite Sprite = GetComponen<SpriteRenderer.().Sprite; 
    spriteSize = Vector3.magnitude(sprite.bounds.extents);// get this only once for efficiency
}

public void TakeDamage(int damage)
{
    int dist = 10;
    Vector3 spawnPos = transform.position + (-1*transform.up) * (dist*spriteSize);
    GroundItem.Create(new Item(3, 1), spawnPos);

    health -= damage
}

EDIT:

As pointed out in the comments transform.forward will be into the game's third dimension, entirely not what you want. -1*transform.up (down direction) should work

\$\endgroup\$
11
  • \$\begingroup\$ It still creates the item center of the enemy \$\endgroup\$
    – Printer
    May 6, 2020 at 7:46
  • \$\begingroup\$ What is the value of spriteSize? \$\endgroup\$
    – Jay
    May 6, 2020 at 8:17
  • \$\begingroup\$ 256x256. I get the magnitude of 1.8 \$\endgroup\$
    – Printer
    May 6, 2020 at 8:46
  • \$\begingroup\$ 1.8 is non-zero, you might need to increase the value of dist but I can't see why that wouldn't work? what is the value of spriteSize for different sprites? \$\endgroup\$
    – Jay
    May 6, 2020 at 8:48
  • 2
    \$\begingroup\$ Note that in the default Unity setup for a 2D game, transform.forward points into the screen, so you might want to displace along -transform.up instead. \$\endgroup\$
    – DMGregory
    May 8, 2020 at 10:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .