- Home /
attack hitting self
i have a character that uses an attack whose hitbox is right in their own, ive tried using an if statement to overcome this, but for some reason it doesnt work?
heres the if statement private void OnTriggerEnter2D(Collider2D other) { if(other.gameObject != gameObject.GetComponentInParent().gameObject) //this is so that it cant hit the parent { gameObject.GetComponentInParent().Hit(other, damage, knockbackSide, knockbackUp, attackType, gameObject.GetComponentInParent().facingLeft); Debug.Log("HitPlayer"); } }
can someone please tell me why this doesnt work, and how do i fix it?
You can typically overcome this using physics layers to make sure someone's attacks can only hit other actors. Unity has a nice video on layers that might be helpful.
bit i already have it on a layer, but i am attacking a seperate player on that same layer. i just dont want it to hit the attacking player. I really just want to know why the if statement isnt working and how to fix it
Answer by Sethery88 · Nov 15, 2019 at 05:30 PM
So I have found the problem. apparently GetComponentInParent<>() works differently than i assumed. the documentation states "Returns the component of Type type in the GameObject or any of its parents." So it appears it returns the component on the current object if it exists and not the parent.
Sorry @Captain_Pineapple you were right, i just understood yours as an alternative rather than a solution
Answer by Captain_Pineapple · Nov 13, 2019 at 07:25 PM
Assuming that you do not have a transform hierarchy above your player character you should perhaps change the if statement to:
if(other.transform.root != transform.root)
or introduce a tag if possible which tags your player as the "player" and do:
if(!other.transform.root.CompareTag("player"))
or as @TreyH mentioned set your player to its own "player" layer and disable player-player collisions in the project physics settings. The last option would be most safe regarding later changes to hierarchys.
the thing is, you will be attacking multiple players, plus i do have a transform component on every player, so i dont see why its not working
But have you used "other.transform"? If so, it should not activate on yourself, unless your "attack" is actually not part of the player itself and just spawns in front of them.
my attack is a child of the player, but i dont see why i should use that, and why $$anonymous$$e isnt working if you could please help me figure out why this isnt working i would appreciate it, i also linked the script files here
https://www.dropbox.com/s/kn5vb61uizgtdgx/AttackHitBoxHandler.cs?dl=0 https://www.dropbox.com/s/hm4wxybx2gixx1x/Player$$anonymous$$ovement.cs?dl=0
Hey, your answer was very helpful. I was creating a multiplayer combat game and player was hitting self. But adding this conditional made it work perfectly. Thanks a lot :-)
Answer by Sethery88 · Nov 14, 2019 at 05:54 PM
also, i there is another error that may be the problem? i dont know.
NullReferenceException: Object reference not set to an instance of an object PlayerMovement.Hit (UnityEngine.Collider2D collision, System.Int32 Damage, System.Int32 KnockbackSide, System.Int32 KnockbackUp, System.String AttackType, System.Boolean FacingLeft) (at Assets/Scripts/PlayerMovement.cs:253) AttackHitBoxHandler.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/AttackHitBoxHandler.cs:28)
here are the links to the 2 scripts in question https://www.dropbox.com/s/kn5vb61uizgtdgx/AttackHitBoxHandler.cs?dl=0 https://www.dropbox.com/s/hm4wxybx2gixx1x/PlayerMovement.cs?dl=0
Your answer
Follow this Question
Related Questions
How To Set Up 3D HitBoxes For Animated Character? 0 Answers
print on screen 1 Answer
Populate an array of GameObjects with collision? 1 Answer
Timing animations with mecanim 0 Answers