#cat game issue

1 messages · Page 1 of 1 (latest)

waxen tendon
#

Give me a sec ill just post the original script in here from u

#

https://hatebin.com/vvijnqsmsa

and you said you've changed OnCollisionEnter2D to be this right?

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Upgrade") && !hasCollidedWithUpgrade)
        {
            maxHealth++;
            currentHealth++;
            Destroy(collision.gameObject);
            hasCollidedWithUpgrade = true;
        }
        else if (collision.gameObject.CompareTag("Rat"))
        {
            Vector2 knockbackDirection = transform.position - collision.transform.position;
            TakeDamage(1, knockbackDirection);
        }
    }
waxen tendon
#

Ill help u add some debugs, but try to understand the thought process behind it as well

odd vigil
#

well firstly i gotta fix this error im getting all of a sudden before i can move on to anything else

waxen tendon
#

Ok first thing, the issue is the cat is instantly dying, no matter what.
Lets test if thats true because theres a few cases:

  1. The cat is just colliding multiple times and processing multiple damage
  2. There are multiple things tagged Rat
  3. There is another script affecting this
#

show your updated code

odd vigil
waxen tendon
#

oh thats a different script i see

odd vigil
#

yeah

waxen tendon
#

show the one with the error

odd vigil
#

idk why its causing an issue now

waxen tendon
#

your TakeDamage script needs an int, and vector 2

odd vigil
odd vigil
waxen tendon
#

show me the logs u wrote

#

especially for 1

#

the logs will show that it took 1 damage yes, but how many times did it take 1 damage?
Its possible it collides, the cat bumps off a little and then with forward velocity collides again

odd vigil
#

yep its taking damage twice i guess

waxen tendon
#

so either thats just naturally happening because the cat or rat (or both) have velocity towards each other and collide twice in this
or there are 2 colliders on the rat (like a body and arm) and both are colliding at once

odd vigil
waxen tendon
#

the best bet would be implement an immunity phase regardless for your player. A lot of games do this, its called either immunity frames, invincibility frames, or "i frames". Like if the player gets hit, dont let them take damage for some time, maybe 0.1 seconds, maybe 1 second

#

any other solution like ensuring they dont collide twice will be really annoying to implement

odd vigil
#

oo okay lemme try

waxen tendon
#

nice, just curious how did u implement it