Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 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 DedWnutry · Jun 06 at 01:31 AM · physicscolliders

How register damage only once per collision?

Straight to the point - i have character with no colliders on him, but my Character Controller acts like one for some reason. When enemy swings his weapon. the collider on weapon enables to deal damage for like 0,1 sec through coroutine and then disabling. I used OnCollisionEnter and OnTriggerEnter in weapon script, so it suppose to register collision only once. But somehow it deals damage multiple times almost randomly. And i can't understand why. When weapon collides, script automatically disables it's own collider and stops coroutine. Pls help, it's very confusing сonsidering my character even don't have any colliders :/ There's my script on weapon:

 public void Attack()
 {
     StartCoroutine(EnableHitbox());
 }

 IEnumerator EnableHitbox()
 {
     yield return new WaitForSeconds(openFrame);

     GetComponent<BoxCollider>().enabled = true;

     yield return new WaitForSeconds(closeFrame);

     GetComponent<BoxCollider>().enabled = false;
 }
 
 void OnTriggerEnter(Collider other)
 {
     if (transform.root.tag == "Enemy")
     {
         int healthDamage = 0;

         if (other.transform.tag == "Player")
         {
             Debug.Log(other.name);
             //GetComponent<BoxCollider>().enabled = false;

             StopCoroutine(EnableHitbox());

             other.transform.root.GetComponent<PlayerStats>().Health(healthDamage = slash);
         }
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by TheSpherinder · Jun 06 at 05:59 AM

First of all, I would recommend giving the player a collider to avoid any issues later, for a lot of things, you need a collider on both objects for the collider to recognise an overlap, otherwise, because there is no second collider for the first collider to collide with, it might as well not be touching anything.
As for the weapon, if it disables it's collider at the end of the script, then it will not be enabled to detect a collision the next time the script runs.
I would suggest changing your code to be something like (this is not actual code):

 private float myTimer;
 
 myTimer = 10;
 
 Fixed Update()
 {
     if (Weapon is colliding with the players collider, once the player has one)
     {
        if (myTimer > 10)
        {
        deal damage to the player;
        myTimer = 0;
     }
      myTimer = myTimer + 1
 //I know there is a quicker way to do that, but I think like that, so X = X+ 1 it is
 
 //so myTimer, is a cooldown, and every time the timer is larger than ten, it sets to 0, and does damage, if it is touching the player
 
   }

And if the player not having a collider is necessary, just remember you can disable collisions with layers for the player in the layer collision matrix.

Comment
Add comment · Show 2 · 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 DedWnutry · Jun 06 at 07:36 AM 0
Share

When i add a coliider to player actually nothing changes, and it's main issue with this bc i can't afford disabling CC - debug prints that there was many collisions despite that was one hit and weapon's collider disables instantly.

i

t might as well not be touching anything.

Strange, bc when enemy weapon collides with character with no colliders, in debug prins out that there's collision with something with tag "player", so i assume it counts it like a legit collision.
A

s for the weapon, if it disables it's collider at the end of the script, then it will not be enabled to detect a collision the next time the script runs.

No, the "Attack" function called from another script, driving enemy's AI, so collider enables every time player enter attack range and it has cooldown. I see what Your code doing, but it's the same as disabling collider after entering first collision. I tried the same with booleans and have same results. Disabling collider with OnCollisionExit also didn't work for me. Sorry for bad english.
avatar image DedWnutry · Jun 06 at 07:40 AM 0
Share

Sorry didn't see the last sentence. Going to google about it now.

avatar image
0

Answer by DedWnutry · Jun 06 at 04:51 PM

I'm alredy figure it out. The issue was within another script - Attack function was activating when enemy's AI selecting the animation attack type randomly. It generates random numbers many times within one frame. Therefore attack function was called multiple times and coroutine enables collider continuosly. So it was nothing with OP script, simply my fault. The solution to this just make exit time for attack animations.

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

221 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 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 stop mesh entering another mesh, both with colliders 3 Answers

Attaching multiple rigidbodies together and spawnning as prefabs 1 Answer

Piling up objects and transporting them 1 Answer

What is "Physics Mesh" and Do I Need Them If Not Using Mesh Colliders? 0 Answers

How to make a table model into something solid 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