#Behavior only works when manually changing values in inspector

1 messages · Page 1 of 1 (latest)

orchid spear
#

in my game, two orbs can clash weapons and in turn would change their orbit direction

    // Collision Handling
    protected virtual void OnTriggerEnter2D(Collider2D col)
    {
        if (owner == null) return;

        // Ricochet
        Weapon otherWeapon = col.GetComponent<Weapon>();
        if (otherWeapon != null)
        {
            Rigidbody2D myOrb = owner.GetComponent<Rigidbody2D>();
            Rigidbody2D otherOrb = otherWeapon.owner.GetComponent<Rigidbody2D>();

            if (myOrb && otherOrb)
            {
                Vector2 dir = (myOrb.position - otherOrb.position).normalized;
                float bouncePower = 8f;

                myOrb.AddForce(dir * bouncePower, ForceMode2D.Impulse);
                otherOrb.AddForce(-dir * bouncePower, ForceMode2D.Impulse);
            }

            // Reverse orbit directions
            orbitDir *= -1f;
            otherWeapon.orbitDir *= -1f;

            Debug.Log($"{name} ricochet! orbitDir={orbitDir}, angle={angle}");

            // Flip both sprites
            if (spriteRenderer != null)
                spriteRenderer.flipX = orbitDir < 0;
            if (otherWeapon.spriteRenderer != null)
                otherWeapon.spriteRenderer.flipX = otherWeapon.orbitDir < 0;

            // Play ricochet sound
            PlaySound(ricochetSound);
            return;
        }

it goes counter clockwise for orbitDir = 1 and clockwise for -1
when debugging, every clash changes the orbit and says what the value is
however, it doesn't change the orbit it at all

but the weird thing is, if i manually change the value during runtime in the inspector, it works
honestly im just so lost on what i could do

waxen pewterBOT
#

success @naaboom muted

Reason: Too many messages with links sent.
Duration: 29 minutes and 53 seconds

Errors

DM not sent: User disabled DMs.

orchid spear
#

awesome

#

id be super thankful for any help, ive been stuck on this for like 10 hours now

old burrow
orchid spear
#

if both actors run the script in the same collision event they may overwrite/cancel each other and/or you may have a race condition.
this was the problem
i fixed it by getting the instance id and just using one of em instead

#

🙏