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 Mortilicious · Dec 23, 2021 at 03:25 PM · collidertriggerbeginner

OnTrigger Enter not working - Tried everything!

I have a project in which I am trying to create combat. I have a Player character in which I spawn the weapon as a child of the players hand. In front of my Player is an enemy.

The problem is that the OnTriggerEnter function never fires. I have an animation in which the weapon swings forward in which the 2 Box colliders intersect. I've slowed down this animation significantly as well to make sure this is the case.

I have read through every post I could find and nothing seems to solve it for me.

The Weapon

  • Has a RigidBody (not 2D)

  • Has a Box Collider (not 2D)

  • Is set to "IsTrigger"

  • Is set to "IsKinematic"

  • Has a Script attached to it (IAMelee.cs) with the code involved

alt text

The Enemy

  • Has a RigidBody (Not 2D)

  • Has a Box Collider (Not 2D)

  • Is NOT set to "IsTrigger"

  • Is NOT set to "IsKinematic"

alt text

The Code in IAMelee.cs

 void OnTriggerEnter(Collider other)
 {
     Debug.Log("Collision");
 }

What am I doing wrong?

enemy.jpg (395.7 kB)
weapon.jpg (410.9 kB)
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 NPnpNPnpNP · Dec 23, 2021 at 04:36 PM 0
Share

Did you tried by making the RigidBody of the weapon non kinematic ?

avatar image Mortilicious NPnpNPnpNP · Dec 24, 2021 at 07:19 AM 0
Share

I did, but didn't seem to work

avatar image Laiken · Dec 26, 2021 at 07:56 PM 0
Share

Just to make sure, check if you did not accidentally disable console notifications. (try using debug.log() on a Start() method to see if it's showing on the console)

avatar image endasil_unity · Dec 29, 2021 at 04:13 PM 0
Share

You seem to do things correctly based on the information available so far. I think it will be hard for us to figure out what is going wrong with only this information. Would you be able to share your project or even better, a $$anonymous$$imal version of it with just things absolutely necessary to reproduce the problem you're having? I'm really curious about what is causing this issue and would like to take a look at the problem.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Waterpolo7 · Dec 23, 2021 at 09:45 PM

Hi @Mortilicious,


this probably won't change much, but what about changing the collision detection in your rigidbody component to "continuous"? I don't really see anything wrong with your components, though. Another solution (which doesn't contain collision voids) would be using a raycast to check if the enemy is in range (to send damage to their script) and just playing the animation without having a collider on your weapon.


I hope this is somewhat useful, feel free to react :)

Comment
Add comment · Show 5 · 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 Mortilicious · Dec 24, 2021 at 07:16 AM 0
Share

Hey @Waterpolo7

I gave it a try, but it doesn't seem to work! I did get a message that a Kinematic Rigidbody can only do speculative continuous, but even then I didn't seem to be able to register anything.

avatar image Waterpolo7 Mortilicious · Dec 24, 2021 at 03:50 PM 0
Share

Hi @Mortilicious I can only suggest you using a raycast then. I think the raycast method should also make sure you avoid the collider not registering while it should be hitting, as I don't think colliders are the best option here. If you insist on using the colliders, you could always try the answer by YagirX; checking your layer matrix in Edit > Project Settings > Physics > Layer Matrix, because some layers could be ignoring each other. If some of those are not marked, then you could try marking all of them and see if it helps and if no other bugs appear because of it. Could you also check what layer both objects are on in their inspector window? Let me know what happens :)

avatar image Mortilicious Waterpolo7 · Dec 26, 2021 at 11:27 AM 0
Share

Hey @Waterpolo7 Thanks for taking the time to look into it :) Glad to see I'm not the only one who's stumped. My Layers are all set to default, so I'm not sure if that's fine? As I was going through the other answers they all said that using the default layers is the easiest way to get things to work, but in case you need more nuance you can play around with them. Considering that its not working now anyway, I figured I shouldn't look at this. Could you emphasize a bit on how you would approach the Raycast method? I know what Raycasts are, but as far as I know they are used to plot a single trajectory between 2 points?

Show more comments
avatar image
0

Answer by YagirX · Dec 24, 2021 at 11:54 AM

Not the fact that it will work, but try to change the type of collision and make the object not kinematic and freeze all the constraints. Perhaps something in the layers was done wrong or fixed Time Step = 0.

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 heislerc0 · Dec 28, 2021 at 11:05 PM

Both GameObjects must contain a Collider component. The trigger must have Collider.isTrigger enabled, and contain a Rigidbody.


So your sword is the Trigger. The OnTriggerEnter(Collider other) function should be written in your Enemy's Script.


It is your enemy that enters the trigger (sword), so you must use the "other" parameter to figure out what the triggers is and act accordingly. You can use tags or check for your IAMelee script.


IAMelee sword = other.gameObject.GetComponent<IAMelee>() Will be null if not the melee object causing the trigger.


other.tag == "melee" Return true or false, but you have to add the tag to your object with the trigger.

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 endasil_unity · Dec 29, 2021 at 04:08 PM 0
Share

It is perfectly fine to have the OnTriggerEnter function on the game object that has the Collider set to trigger. I use it like that in my game.

avatar image heislerc0 · Dec 29, 2021 at 09:29 PM 0
Share

It did not work for me like that last time I tried. I would recommend they still try this solution.

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

211 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

Related Questions

How to change a texture of a gameobject by collider trigger ? 1 Answer

Can't click gameobject when over another trigger? 1 Answer

How to SetActive an object with a raycast? 2 Answers

Why is my trigger collider not working? 2 Answers

Make an object a trigger? 1 Answer


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