• 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
1
Question by unity_943kingpl · Feb 18, 2020 at 02:38 PM · physics

How can you have one continuos collision over multiple objects

Im working on a game where you are falling through some kind of corridor and need to avoid stuff while falling
The level generation works that it takes a random prefab out of an array and instantiates it below the last one so the walls are just a lot of different objects stacked on top of each other
and when i tried to implement dying I realized that I can´t use the "OnCollisionEnter2D()" function to reduce the health of the Character because it would be loosing health every time it comes past a new piece of wall

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 JPhilipp · Feb 18, 2020 at 03:32 PM 0
Share

What are the specific circumstances in which you want to reduce health, and what are they when you don't want to? And do you have an image of the current setup in action?

avatar image unity_943kingpl JPhilipp · Feb 18, 2020 at 04:05 PM 0
Share

The Problem is that even thuogh I set the health to for example 5 you die before you touched the obstacles 5 times because the health is reduced multiple times because you touch multiple objects

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by CurtisGM · Feb 18, 2020 at 11:32 PM

Hello @unity_943kingpl !

FullMe7alJake7's answer is a good solution and should work for you! Another idea for you is to have a singular variable keeping track of the amount of seconds it's been since the player last took damage. From there, whenever the player hits a wall, before they take damage, quickly check to see if the last time they took damage was - say - at least 5 seconds ago. If it was, then it's been long enough that the player should take more damage. Below is an example of this implementation:

 private float timeSinceLastDamage = 0f;
 
 public void Update() {
      timeSinceLastDamage += Time.deltaTime;
 }
 
 private void TakeDamage() {
      if (timeSinceLastDamage >= 5f) {
           player.hp -= 10;
           timeSinceLastDamage = 0f;
      }
 }

Hopefully this helps!

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 FullMe7alJacke7 · Feb 18, 2020 at 10:44 PM

Simply add a counter and bool check to the OnCollision code.

I do this exact thing on my projectile's to avoid damaging unintended targets. The following code is from my projectile script to give you an idea of what I mean.

         public void Activate()
         {
             hasHasCollided = false;
             collisions = 0;
             currentLifeTime = 0f;
             gameObject.SetActive(true);
             if (!hasHasCollided && Data.Lifetime > currentLifeTime)
                 behavior.Perform(this);
         }


         private void OnCollisionEnter(Collision other)
         {
             collisions++;
             hasHasCollided = true;
             Behavior.End(this);
             if (collisions <= 1)
             {
                 Collided?.Invoke(this, new ProjectileCollisionEventArgs(other.gameObject.transform.GetInstanceID(), projectileData.Value));
             }
         }

Your implementation may end up different from mine but the concept remains.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

202 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

Related Questions

2D 360 degress platformer example needed 0 Answers

Calculating trajectory of elliptical orbit? 2 Answers

Calculating a projectiles angle required to hit object. 2 Answers

Is there a way to automatically make physics collider frictionless on sides but not on top? 2 Answers

Simulator physics - how to get objects to "stick" to each other 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