Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sethery88 · Nov 13, 2019 at 05:56 PM · hitboxattacking

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TreyH · Nov 13, 2019 at 06:47 PM 0
Share

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.

avatar image Sethery88 TreyH · Nov 14, 2019 at 06:43 PM 0
Share

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sethery88 · Nov 14, 2019 at 05:11 PM 0
Share

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

avatar image andrewsmithparkside Sethery88 · Nov 14, 2019 at 05:23 PM 0
Share

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.

avatar image Sethery88 andrewsmithparkside · Nov 14, 2019 at 05:37 PM 0
Share

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

Show more comments
avatar image developersam7 · Jan 04 at 05:46 PM 0
Share

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 :-)

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

118 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

I need help deciding the best way to pass different vector values to my hit-boxes and hit-forces depending on which attack is being used !! please help 0 Answers

Timing animations with mecanim 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges