#Collider2D

1 messages · Page 1 of 1 (latest)

eternal yacht
#

@fluid cairn That's the script in its current, working state

fluid cairn
#

ok uh

#

I meant like

#

In its not working state

#

when you were getting the other doesnt exist type stuff

eternal yacht
#

Well, like if I do void Swing(GameObject other) where it currently is

fluid cairn
#

uh huh

eternal yacht
#

If I define Swing earlier then try to call it on mouse click i'd get the 'out of context' error

strong cobalt
#

Then show that code

fluid cairn
#

yeh

strong cobalt
#

I don't know how you expect anyone to fix code you won't show

fluid cairn
eternal yacht
fluid cairn
#

It would be easier if you just sent a codeblock since im on like mobile and stuff

strong cobalt
#
void Swing(GameObject other)
    {
    
        _source.PlayOneShot(_wack1);
            //Animation could be applied to a weapon sprite later
    }

This creates a variable called other that exists only inside of Swing. You can't use it anywhere else

fluid cairn
#

Wait you wanna call it here?

#

Wot

eternal yacht
#

Honestly, I think I'm approaching the whole issue wrong

fluid cairn
#

I thought we were only talking like OnTrigger stuff

strong cobalt
#

And now, everwhere you call Swing, you have to pass it a value for other

eternal yacht
#

Well, I'm trying to apply damage to the target by the ApplyDamage func I defined in an interface

#
public class Bullet : MonoBehaviour
{
    
    public GameObject hitEffect; //Meant for VFX later
   
   
    void OnCollisionEnter2D(Collision2D other) 
    {
        Target target = other.gameObject.GetComponent<Target>();

        if (target != null)
        {
            target.ApplyDamage(1);
        }

       Destroy(gameObject);
    }


    void Start()
 {
     StartCoroutine(SelfDestruct());
 }
 IEnumerator SelfDestruct()
 {
     yield return new WaitForSeconds(5f);
     Destroy(gameObject);
 }
   
}

#

Cus see, on the script for bullets you can see ApplyDamage being used correctly

#

The core issue is wanting to call ApplyDamage on the target defined by OnTriggerStay2D

#

Trying to treat it like the way Bullet handles it is super hamfisted I think

#

So now I gotta try and figure out how to call ApplyDamage on whatever target is activating OnTriggerStay2D

fluid cairn
eternal yacht
# fluid cairn why do you think so?

I dunno man, I've been learning a lot the past couple days since I started out and doing it like that just feels wrong now that I think about it

#

also hell yeah thanks for letting me know about that, dunno why I never bothered looking up the parameters for Destroy lol

fluid cairn
#

np

#

id also do ```cs
if (other.gameObject.TryGetComponent(out Target target)) {
target.ApplyDamage(1);
}

#

other than that idk, i dont really see anything wrong with doing it in OnTriggerStay, other than the fact that it runs every physics update, so youll get damaged many many times, so you'll have to circumvent that somehow if you dont want it

eternal yacht
#

I'm going to try this real quick ```cs
void OnTriggerStay2D(Collider2D other)
{
if(other.gameObject.tag == "enemy")
{
targetingHostile = true;
}

    if(Input.GetButtonDown("Fire1"))
    {
        if(targetingHostile == true && meleeEnabled == true)
        {
        Target target = other.gameObject.GetComponent<Target>();
        target.ApplyDamage(1);
        Swing();
        }
        else
        {
            if(meleeEnabled == true) 
            {
                Swing();
            }
        }
    }
    
    
}

void Swing()
{

    _source.PlayOneShot(_wack1);
        //Animation could be applied to a weapon sprite later
}
#

Okay, actually partial success

#

Now I just have to rig a way for that one shot to be played when you're not targeting someone WITHOUT calling it when not in melee

#

Adding ```cs
if(Input.GetButtonDown("Fire1") && meleeEnabled == true)
{
_source.PlayOneShot(_wack1);
}

#

but i think I know why, if I can have that mousebutton change set a bool to true while pressed then false when released

#

welp, time to look at the Input documentation

#

Okay! That also sorta worked. Mouse1 toggles a bool clicking and the attack script looks for that to be true instead of mouse input itself