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 Michael 12 · Apr 30, 2012 at 06:03 PM · collidersif elseenemy damageplayer damage

Why is my collider not sending damage?

OK I have my mouse trap set up just like everything else but it's not working?

 static var enemyHit : boolean =false;    // toggle for attack mode
 static var damageAmount:int;    // hold player damage amount

 function OnTriggerEnter ( other : Collider ) {                                        // trigger events for collider on foot
     if ( other.tag == "enemy" )                                                     // if collider equals enemy object
     {
         enemyHit = true;                                                            // enable attacking 
         damageAmount = other.GetComponent ( EnemyController ).damageAmount;            // take damage to player health
         yield WaitForSeconds ( 1 );                                                    // wait a second before checking for another hit
     }
     else if ( other.tag == "robot" )                                                     // if collider equals enemy object
     {
         enemyHit = true;                                                            // enable attacking 
         damageAmount = other.GetComponent ( AiPatrol ).damageAmount;                // take damage to player health
         yield WaitForSeconds ( 1 );                                                    // wait a second before checking for another hit
     }
     else if ( other.tag == "mouseTrap" )                                             // if collider equals enemy object
     {
         enemyHit = true;                                                            // enable attacking 
         damageAmount = other.GetComponent ( MouseTrap ).damageAmount;                // take damage to player health
         yield WaitForSeconds ( 1 );                                                    // wait a second before checking for another hit
         print ("Player was hurt by a Mouse Trap");
     }
 
 }
 function OnTriggerExit  ( other : Collider ) {                                        // check for collider exiting
     if ( other.tag == "enemy" )                                                        // if tag enemey exits
     {
         enemyHit = false;                                                            // disable enemy hit ability
     }
     else if ( other.tag == "robot" )                                                        // if tag enemey exits
     {
         enemyHit = false;                                                            // disable enemy hit ability
     }
     else if ( other.tag == "mouseTrap" )                                                        // if tag enemey exits
     {
         enemyHit = false;                                                            // disable enemy hit ability
     }
 }


I figured maybe it might help to see this also Mouse Trap Script

 var mouseTrapSnap                         : AudioClip;                                // Assign Mouse Trap Snap Audio
 var playerHit                             : boolean         = false;                    // enable player hit
 var damageAmount                         : int             = 25;
 
 
 function Start () {
     animation.wrapMode                 = WrapMode.Once;    // set play animation mode to once
     
     animation [ "snap" ].wrapMode     = WrapMode.Once;    // set animation to play once
 }
 
 
 function OnTriggerEnter ( other : Collider ) {            // if collision with player
     //var   controller : CharacterController = GetComponent(CharacterController);
     if ( other.tag == "Player" )
             {
             //Play Mouse Trap Snap Sound
             audio.PlayClipAtPoint ( mouseTrapSnap, transform.position );
             yield WaitForSeconds (0.2);
             //Play Mouse Trap Snap Animation
             animation.Play ( "snap" );
             }
     else if ( other.tag == "push" )
             {
             //Play Mouse Trap Snap Sound
             audio.PlayClipAtPoint ( mouseTrapSnap, transform.position );
             yield WaitForSeconds (0.2);
             //Play Mouse Trap Snap Animation
             animation.Play ( "snap" );
             //Destroys Character Controller);
             //Destroy(controller);
                //dissable Collider by Destroying it
             Destroy(collider);
             }
 }
 
 function OnTriggerExit ( other : Collider ) {
     //var   controller : CharacterController = GetComponent(CharacterController);
     if ( other.tag == "Player" )
             {
             yield WaitForSeconds (0.2);
             //Destroys Character Controller);
                //Destroy(controller);
                //dissable Collider by Destroying it
             Destroy(collider);
             }
     
 }

Comment
Add comment · Show 8
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 Seth-Bergman · Apr 30, 2012 at 06:12 PM 0
Share

is it entering the "if ( other.tag == "mouseTrap" )" at all? Are you sure the gameObject is actually tagged, maybe not tagged or spelled differently.

avatar image fafase · Apr 30, 2012 at 06:16 PM 0
Share

I allowed myself to make this look better.

avatar image fafase · Apr 30, 2012 at 06:17 PM 0
Share

Now what is not working? That would help us give you a faster and efficient answer if you let us know what is wrong.

avatar image Michael 12 · Apr 30, 2012 at 08:58 PM 0
Share

yes the mouse trap in tagged "mouseTrap" yes the box collider is of a fair size to cover the whole mouse trap. I know he is reacting to it because in the "Controller System" script I have another code that makes him jump back and make a sound when he collides with the box collider on the mouse trap. The other thing is that he sometimes takes damage but most times not?

Thanks fasase for cleaning up my mess ;)

avatar image Seth-Bergman · Apr 30, 2012 at 09:37 PM 0
Share

is the message printing? or is the damage the only problem? also, another stupid question, you made sure isTrigger is checked (I assume yes). Also, do you mean to set player damage amount equal to the others, or rather subtract from it (like hit points) as in: damageAmount -= other.GetComponent ( AiPatrol ).damageAmount;

Show more comments

3 Replies

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

Answer by Seth-Bergman · May 01, 2012 at 01:52 AM

Wait, you are saying the trap animation plays.. OK, so I would put it all in one, like:

 var playerHit : boolean = false; // enable player hit var damageAmount : int = 25;
 
 function Start () 
 { animation.wrapMode = WrapMode.Once; // set play animation mode to once
 
 animation [ "snap" ].wrapMode   = WrapMode.Once; // set animation to play once
 
 }
 
 function OnTriggerEnter ( other : Collider ) { // if collision with player 
 
 if ( other.tag == "Player" ) { //Play Mouse Trap Snap Sound 
 audio.PlayClipAtPoint ( mouseTrapSnap, transform.position );
  yield WaitForSeconds (0.2); //Play Mouse Trap Snap Animation 
 animation.Play ( "snap" ); 
 
 var otherScript = other.GetComponent(PlayerScript); // access player from trap
 
 otherScript.damageAmount += damageAmount;
 otherScript.enemyHit = true;
 
 
 }
 else if ( other.tag == "push" ) { //Play Mouse Trap Snap Sound 
 audio.PlayClipAtPoint ( mouseTrapSnap, transform.position );
  yield WaitForSeconds (0.2); //Play Mouse Trap Snap Animation 
 animation.Play ( "snap" );
  } }
 
 function OnTriggerExit ( other : Collider ) {
  if ( other.tag == "Player" ) {  
  Destroy(collider); }
 
 }
Comment
Add comment · Show 4 · 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 Seth-Bergman · May 01, 2012 at 01:56 AM 0
Share

also, not having the character controller is, I'm sure, causing the problem. did you adjust the script for that, it's hard to tell by the example.. not that you would need a character controller for this, but the script was looking for one (though i think you commented that out).. either way, not sure why.. maybe it has something to do with the signal being sent to the rigidbody, rather than the collider. Script reference seems to suggest this.

avatar image Michael 12 · May 01, 2012 at 02:08 AM 0
Share

Sweeeeetness!! Now that worked!! Thanks Seth, problem solved :)

avatar image Seth-Bergman · May 01, 2012 at 02:13 AM 0
Share

cool glad to help. $$anonymous$$oving forward, you could just create a function on the player, then call that the same way like:

otherScript.DoWhatever();

glad I could help!

avatar image Michael 12 · May 01, 2012 at 04:04 PM 0
Share

Thanks Seth, this will help as I've no doubt I'll be creating many more dangerous obstacles like the trap that are animated and cause damage to the player if collided with :)

avatar image
0

Answer by bompi88 · Apr 30, 2012 at 06:32 PM

The mouseTrap prefab has a well sized collider, and it is tagged as "mouseTrap"? one part rhetorical question, one part not.

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 Seth-Bergman · May 01, 2012 at 12:45 AM

If the trigger is being entered by the trap, then you could always call the other object from that same function, as in :

 var otherScript = other.GetComponent(PlayerScript);
 otherScript.damageAmount += damageAmount;
Comment
Add comment · Show 7 · 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 Michael 12 · May 01, 2012 at 12:48 AM 0
Share

The player enters a trigger collider attached to the Trap.

avatar image Seth-Bergman · May 01, 2012 at 12:52 AM 0
Share

so does the trap snap? If so, add the lines above to the trap's trigger function, and it will handle both sides at once..

avatar image Michael 12 · May 01, 2012 at 01:03 AM 0
Share

The mouse trap plays a snap animation when the player triggers the collider. Not sure what you mean by that last bit? The players damagage is handles by a "Controller Collider hurt" script, very short and simple script.

else if ( other.tag == "mouseTrap" ) // if collider equals enemy object { enemyHit = true; // enable attacking damageAmount = other.GetComponent ( $$anonymous$$ouseTrap ).damageAmount; // take damage to player health yield WaitForSeconds ( 1 );

avatar image Seth-Bergman · May 01, 2012 at 01:11 AM 0
Share

And again, the exact issue is still unclear.. Is the trigger function getting entered at all, by either object? Sounds like what you're saying is the trap is working, but not the player. In which case you should just send a message to the other object from the one that works.

avatar image Michael 12 · May 01, 2012 at 01:38 AM 0
Share

Well, I'll try and explain as best I can. I made some additional changes and now have it where my player can roll a ball over the trap and that will set the trap off and remove the box collider, so that is working great, even my player when he enters the trap removes the box collider on the trap which is good so I know that, that part works. But his damage is not being either registered or sent which I just don't get at all because you can see from the script that the trap is set up the same as the robot and the enemy, the only real difference is that the trap does not have or need a character controller, which I just can;t see that being the problem.

Show more comments

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

7 People are following this question.

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

Related Questions

physics.OverlapSphere colliders 1 Answer

Does unity treat colliders differently if outside of camera frustrum 0 Answers

Flying Controls Collision Issues 0 Answers

How to do static collision checks? 1 Answer

Why there is no Collider.IsTouching(...) ? 2 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