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 MerlinsMaster · Jun 30, 2016 at 05:06 AM · collision detectionbooleans

Can't get this bool variable to switch on inside my OnCollisionEnter function

Hey guys,

I have the following C# script that detects collisions on a specific collider. In it, I have a bool variable called hitDetected01, which when true, signals another script to add the damage amount calculated in this script to the damage total in the other script. My problem is that the hitDetected bool is not showing as true when it is supposed to, and I am having considerable difficulty figuring out why.

Can anyone take a look and tell me what I'm doing incorrectly?

 using UnityEngine;
 using System.Collections;
 
 public class cs_damageSec01 : MonoBehaviour 
 {
     // hitDetected bool
     public bool hitDetected01;
 
     public float damSecAmount01;
 
     void Start () 
     {
         // a collision is not detected by default
         hitDetected01 = false;
     }
 
     // *** DETECT COLLISIONS WITH OTHER OBJECTS - CALCULATES COLLISION DAMAGE ***
     void OnCollisionEnter(Collision collisionInfo)
     {
         // the following code ensures that only one collision detection occurs per call
         if (hitDetected01)
             return;
         hitDetected01 = true;
 
         // detects the collision
         float rvX = collisionInfo.relativeVelocity.x;
         float rvY = collisionInfo.relativeVelocity.y;
         float rvZ = collisionInfo.relativeVelocity.z;
 
         // calculates impact velocity
         float velocityMod = Mathf.Abs(rvX) + Mathf.Abs(rvY) + Mathf.Abs(rvZ);
 
         // calculates damage amount
         damSecAmount01 = (Random.value * 5) * velocityMod;
 
         
         Debug.Log("collision object 01 hit");
 
         
 
     }
 
     void Update () 
     {
         // hitDetected bool returns to its default state
         hitDetected01 = false;
 
         
     }
 

The other script it communicates with:

 public class cs_playerShipDamage : MonoBehaviour {
 
     // if a hit is detected
     // record damage amount
     // deduct from affected health bar
     // calculate what systems are affected
     // assign damage to those systems
 
 
     public float damSecHealth_01 = 100f;
     public float damSecHealth_02 = 100f;
     public float damSecHealth_03 = 100f;
 
     public GameObject damSecGO_01; // collider object 01
     public GameObject damSecGO_02; // collider object 02
     public GameObject damSecGO_03; // collider object 03
 
     private cs_damageSec01 ref_cs_damageSec01;  // collider script : collider script reference
     private cs_damageSec02 ref_cs_damageSec02;  // collider script : collider script reference
     private cs_damageSec03 ref_cs_damageSec03;  // collider script : collider script reference
 
     public bool hitPositive_01 = false;   // variable that determines if a collider is detecting a hit  
     public bool hitPositive_02 = false;   // variable that determines if a collider is detecting a hit 
     public bool hitPositive_03 = false;   // variable that determines if a collider is detecting a hit 
 
 
     void Start () {
 
         
 
         ref_cs_damageSec01 = damSecGO_01.GetComponent<cs_damageSec01>();
         ref_cs_damageSec02 = damSecGO_02.GetComponent<cs_damageSec02>();
         ref_cs_damageSec03 = damSecGO_03.GetComponent<cs_damageSec03>();
 
         hitPositive_01 = false;
         hitPositive_02 = false;
         hitPositive_03 = false;
 
     }
 
     
 
     // *** TALLIES UP DAMAGE AND HEALTH VALUES - DESTROYS SHIP IF OUT OF HEALTH ***
     void Update () {
 
         // 
 
         if (ref_cs_damageSec01.hitDetected01 == true)
         {
             hitPositive_01 = true;
             Debug.Log("Section 01 Taking Damage.");
             damSecHealth_01 += -ref_cs_damageSec01.damSecAmount01;
         }
 
         hitPositive_02 = false;
 
         if (ref_cs_damageSec02.hitDetected02 == true)
         {
             hitPositive_02 = true;
             Debug.Log("Section 02 Taking Damage.");
             damSecHealth_02 += -ref_cs_damageSec02.damSecAmount02;
         }
 
         hitPositive_02 = false;
 
         if (ref_cs_damageSec03.hitDetected03 == true)
         {
             hitPositive_03 = true;
             Debug.Log("Section 03 Taking Damage.");
             damSecHealth_03 += -ref_cs_damageSec03.damSecAmount03;
         }
 
         hitPositive_03 = false;
 
         
 
     }
 }

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

1 Reply

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

Answer by fafase · Jun 30, 2016 at 05:28 AM

You set it to false in Update so even though a collision is detected in Physics update, you set it back to false in that same frame.

You may have some scripts that actually see it as true if Unity ordered them so they would happen first.

My solution would be to reset it in LateUpdate so all scripts using the variable in Update have run.

Best solution is to actually propagate the info via event and listener.

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 MerlinsMaster · Jun 30, 2016 at 06:46 AM 0
Share

Thanks. That worked perfectly. However, I didn't understand what you meant in the last sentence:"Best solution is to actually propagate the info via event and listener."

avatar image fafase MerlinsMaster · Jun 30, 2016 at 08:39 PM 0
Share

Ins$$anonymous$$d of setting a variable and having other classes checking in update if it is true, you would use an event/listener system where all the classes that need to know about that class state would register to an event and when the collision is called, that event is triggered and tells all listener about it.

Too long and irrelevant to explain in details here, so you should look into an event/listener tutorial, quite a few on the internet.

avatar image MerlinsMaster fafase · Jul 02, 2016 at 05:08 AM 0
Share

Thanks. I'm definitely going to look into that.

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

43 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

Related Questions

Raycast passes Collider 0 Answers

detecting collision of collided object 2d 1 Answer

Weird Collision Detection with a Cube and OnCollisionEnter 2 Answers

How to make a cube (go to a certain position) after colliding with a plane? 1 Answer

[2D] Getting particles to collide with a game object(Explosion debris effect) 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