Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Jotakuun · Mar 05, 2015 at 10:17 AM · collidertriggerraycastingsystemfighting

What's better for fighting system?, Trigger or Raycast?

Hello Unity Community!

I'm making a 2D hack n' slash videogame. I was making the fighting system with box colliders(2D) which are triggers. For example, I put a trigger in every enemies's attack animation which it's going to be able for 0.1 seconds. So everytime this animations plays it's going to do damage to the player if player's collider detect it. (By OnTriggerEnter2D function)

The problem is this system isn't working at all. Sometimes doesn't detect the collision so when enemy attacks 10 times the player only recieve 8-9.

I'm almost mewbie on this and I didn't find a correct way to do it. Someone can help me? Which is the right way to make a good hit detection? It would be better if I use a raycasting system?

Comment
Add comment
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

3 Replies

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

Answer by AdamAz · Mar 05, 2015 at 11:18 AM

Hi

Did you try to extend the lifetime of the collider to confirm that the collision actually takes place?

Did you try putting the enemy collider on a different layer (one that only collides with the player) to be sure that the collision hits the player only?

I personally use collision detection and did not experience the issues you mentioned.

Side note: I think the detection should be made on the enemy side not on the players side, this will allow you to keep logic where the action is rather than keep it all in the player

Comment
Add comment · Show 3 · 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 Jotakuun · Mar 05, 2015 at 12:07 PM 0
Share

It works extending the lifetime of the collider but I tried the same yesterday and didn't works because I didn't extend enough.

Now another problem arise, when I attack at the same time the enemy hits me twice in one attack(It's like the collider collides with two object). And I don' know why because the enemy's hit collider it's in a layer that only collides with the player, and the player's hit collider it's in a different layer that only collides with enemy. What's wrong so?

These are the layers: Player, Enemy, EnemyHit and PlayerHit. EnemyHit only collides with Player, and PlayerHit with Enemy.

Thank you for your help!

avatar image AdamAz · Mar 05, 2015 at 02:18 PM 0
Share

$$anonymous$$aybe there is a second collision which you do not see? Try disabling the enemy attack collider right after the collision was detected, something like:

     void OnCollision2D()
     {
         this.attackCollider.enabled = false
             ... // rest of your logic
     }
 
avatar image Jotakuun · Mar 05, 2015 at 09:23 PM 0
Share

Fixed! Thank you!

avatar image
1

Answer by Woj_Gabel_FertileSky · Mar 05, 2015 at 10:44 AM

Also you could use animation events. Set an aevent in animation settings at specific time, and then choose a method that you want to run at this specific time. Could be a raycast for example. Or, if you dont need a point of impact, just calculate if it hit or not. I did not test this for performance, so there could be faster methods.

alt text


zrzut-ekranu-2015-03-05-o-114406.png (11.5 kB)
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
0

Answer by adsamcik · Mar 05, 2015 at 10:33 AM

With triggers, you should cache what players are currently in range (collision). This can be done easily by adding them to list/array OnTriggerEnter2D and OnTriggerExit2D. That way when player slashes, you go through the list and hit everything in it. I think triggers are a good way of doing this.

This would work only with static colliders, if you want to really detect when weapon has hit the enemy, you should set rigidBody2D collision detection to continous and it should work flawlessly, but it will cost a little more performance to count the collision. With this method you don't need to cache anything, you just detect OnTriggerEnter2D on the player that has been hit.

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

23 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

Related Questions

Collision detection accuracy issues. 1 Answer

Why does this script check for raycast collision? Is it necessary? 1 Answer

Can't click gameobject when over another trigger? 1 Answer

Trigger-based vehicle ai? 1 Answer

Trigger Spawning? 1 Answer


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