#Collider2D
1 messages · Page 1 of 1 (latest)
ok uh
I meant like
In its not working state
when you were getting the other doesnt exist type stuff
Well, like if I do void Swing(GameObject other) where it currently is
uh huh
If I define Swing earlier then try to call it on mouse click i'd get the 'out of context' error
Then show that code
yeh
I don't know how you expect anyone to fix code you won't show
and when you change it to this you gotta have all places where you call Swing to include the parameter
It would be easier if you just sent a codeblock since im on like mobile and stuff
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
Honestly, I think I'm approaching the whole issue wrong
I thought we were only talking like OnTrigger stuff
And now, everwhere you call Swing, you have to pass it a value for other
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
why do you think so?
Also no need for the IEnumerator, Destroy already has a delay parameter so you can just do Destroy(gameObject, 5f) and it will do the same
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
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
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