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
4
Question by Statement · Dec 22, 2010 at 10:21 AM · triggercharactercontrollerdamageontriggerstay

How can I make an enemy hurt the player?

I am working on a simple prototype where I want to use a trigger box collider to cause damage to the player. The player uses a character controller. This trigger is attached to an enemy that walks around. My intent is to damage the player when it enters this trigger. It just turns out that OnTriggerStay won't fire if the player is standing still (if the enemy walks onto the player). I guess this might seem a bit backward but I'd really want to have a "damage trigger" attached to the enemy just because it feels natural. This won't work because it seems OnTriggerStay won't fire unless the player moved into the collider, not the other way around.

  • What do you suggest I'd do instead?

Also, the docs say:

This message is sent to the trigger and the collider that touches the trigger. Note that trigger events are only sent if one of the colliders also has a rigidbody attached.

I don't have any rigidbody though, but it surely responds to my character controller. Is it a fault in the docs or am I using OnTriggerStay wrong?

// My damage trigger code, that doesn't work if the player is standing still.
public class DamageTrigger : MonoBehaviour 
{    
    void OnTriggerStay(Collider collider)
    {
        collider.SendMessage("OnDamage", SendMessageOptions.DontRequireReceiver);
    }
}


Below are my status updates:

As suggested, I have tried using OnTriggerEnter and OnTriggerExit but it doesn't help.

// My damage trigger code, that doesn't work if the player is standing still. public class DamageTrigger : MonoBehaviour { List<Collider> colliders = new List<Collider>();

 void Update()
 {
     foreach (var collider in colliders)
     {
         collider.SendMessage("OnDamage", 
             SendMessageOptions.DontRequireReceiver);
     }
 }

 void OnTriggerEnter(Collider collider)
 {
     colliders.Add(collider);
 }

 void OnTriggerExit(Collider collider)
 {
     colliders.Remove(collider);
 }

}

Comment
Add comment · Show 4
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 Proclyon · Dec 22, 2010 at 10:56 AM 0
Share

OnTriggerStay won't fire unless the player moved into the collider, not the other way around. Thank you, I needed to know that for my own project! Never thought of that and it actually is a great deal of help to me! Thank you!

avatar image Jesus_Freak · Dec 22, 2010 at 12:42 PM 0
Share

isn't it great when someone elses problem gets fixed, and their problem is your solution? that's when everyone's happy!

avatar image The_r0nin · Dec 22, 2010 at 02:09 PM 1
Share

It's a matter of perspective, really. Even though your game makes it appear that the enemy is hurting the player, code-wise, the player is suffering from proximity to the enemy... so it makes sense that the trigger code should be player-based. I make that mistake all the time, thinking in terms of "what is happening (from the perspective of the game player)" rather than "what is the game doing". As for the solution, you might have to code a proximity-based backup with a damage-limit (to keep the damage from doubling up)...

avatar image Statement · Dec 22, 2010 at 02:13 PM 1
Share

"so it makes sense that the trigger code should be player-based" - So, that means that if the enemy stands still (as opposed to the player) then my player wont hurt ins$$anonymous$$d. I think I have got to ditch the trigger idea entirely.

3 Replies

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

Answer by Statement · Dec 11, 2013 at 12:12 AM

Thought I'd clean up my old questions.

A trigger moving into another trigger must also have a rigidbody.

Any physics object that is moving must have a rigidbody for its callbacks to trigger.

Also, moving objects with a collider (trigger or not) that does not have a rigidbody will incur a performance penalty since Physx will have to rebake the entire scenes static physics data. Colliders without rigidbodies fall into that category.


  • What I should have done back then was to ensure both moving triggers had a rigidbody on them.

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
2

Answer by Dwair · Dec 22, 2010 at 02:56 PM

Have you tried with another approach? don't use OnTriggerStay, but OnTriggerEnter and OnTriggerExit.

When entering the trigger, use SendMessage to tell the player object to change into a "hurting continously" status. When exiting that trigger, use SendMessage to end that "hurting" status.

That way the main code is on player, but the triggers remain useful enough ;)

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 Statement · Dec 22, 2010 at 03:12 PM 0
Share

I'll try this right now.

avatar image Statement · Dec 22, 2010 at 03:36 PM 0
Share

Actually, this has the same problem. When moving the trigger into a collider, Trigger messages doesn't seem to fire.

avatar image Dwair · Dec 24, 2010 at 10:56 AM 0
Share

interesting... If your trigger seems not to work, try to analyze the situation: There are two possible problems, the trigger isn't working (so you should make sure your player has a collider and your trigger actually looks for collisions in the same layermask) Or the Send$$anonymous$$essage doesn't work, you made sure your Send$$anonymous$$essage is sent to the right gameobject? Debug.Log(); is really useful there :P

Other things to try is that the collider actually tests if the object entering the trigger is actually the player. $$anonymous$$aybe other gameobject is interfering with your game logic.

avatar image Dwair · Dec 24, 2010 at 11:01 AM 0
Share

I saw you say in another post that you're moving the trigger towards your standing player? mmhh, that can be troublesome...try pinpointing the problem with my earlier comment...if anything else fails, you should try to use Physics.Raycast(); it can behave just like a sphere trigger in fact!

avatar image
0

Answer by 4s4 · Aug 07, 2012 at 12:25 PM

I bypassed this by calculating the distance between the trigger and player object.

"if the distance is less than collisionbox = triggered"

dunno how efficient, but it works.

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

2 People are following this question.

avatar image avatar image

Related Questions

Does a mesh collider animate? 0 Answers

OnTriggerStay Question 0 Answers

My player HP are get decreased from several GameObjects that has is Trigger on 0 Answers

Damage over time on delay 1 Answer

[fixed] OnTriggerExit never called if I change Character Controller center in-script... 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