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 GEWLAR · Oct 31, 2013 at 12:16 PM · javascriptcolliderontriggerenter

OnTriggerEnter (JS)

I have a script for an object. If the object hits my player it should load an other screen. The problem is, it only works when the player is moving. If the player doesn't move the application doesn't load.

Here is what I have:

 function OnTriggerEnter(otherObj: Collider){
 
         if (otherObj.tag == "Player"){ 
             Application.LoadLevel("deathscreen");
         }
 }

Hope you know what the problem is.

Comment
Add comment · Show 9
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 vexe · Oct 31, 2013 at 12:17 PM 1
Share

Use OnTriggerStay ins$$anonymous$$d. - OnTriggerEnter fires only once, only when you first enter the trigger - i.e. it doesn't fire while you're standing inside the trigger.

avatar image ArkaneX · Oct 31, 2013 at 12:34 PM 1
Share

@vexe - I don't think the problem is related to enter/stay. Please note, that before OnTriggerStay OnTriggerEnter should be called, so level should be loaded.

I don't know what happens if one collider is spawned inside other - maybe in this case OnTriggerEnter is omitted?

@GEWLAR - do you have any special code executed when your player starts/stops moving?

avatar image vexe · Oct 31, 2013 at 12:37 PM 0
Share

Yes, you are correct. I read the question in a hurry. I thought about my comment after I wrote it, OnTriggerEnter should do the trick. @GEWLAR try OnTriggerStay, if it works, it means that OnTriggerEnter is getting ignored, in a way. And don't hesitate to show more relevant code.

avatar image GEWLAR · Oct 31, 2013 at 12:42 PM 0
Share

I tryed OnTriggerStay it doesn't work. I think the problem is that the function won't get called if the collider hits the player. It only gets called when the player hits the collider. But how to fix that ?

avatar image vexe · Oct 31, 2013 at 12:46 PM 0
Share

From what I know, it shouldn't matter who hits whom ^_^ - As long as there's a collision detection script/handler attached to either of the colliders.

Show more comments

1 Reply

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

Answer by clunk47 · Oct 31, 2013 at 05:06 PM

You're probably using a CharacterController.

OnControllerColliderHit

If you want to use regular collision detection or trigger on a character it needs another collider component in addition to the charactercontroller, charactercontroller only detects collisions while moving. You could always attach a child collider to the controller and put your collision script on that child. If you're using OnTriggerEnter, be sure one of the objects involved has a rigibody attached.

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 sdgd · Oct 31, 2013 at 05:42 PM 0
Share

OFC I was missing the rigid body, ... probably he did too, ...

avatar image clunk47 · Oct 31, 2013 at 05:52 PM 1
Share

I actually doubt he was missing the rigidbody, since he's saying "It only works when the character is moving", which means it's detecting the collision when Character is moving. That's why I'm guessing he's using a CharacterController, since that's the only collider type in Unity that detects collision ONLY while moving.

avatar image GEWLAR · Nov 01, 2013 at 09:23 AM 0
Share

Yes I'm using a CharacterController. I will try it with an normal Collider soon.

avatar image GEWLAR · Nov 07, 2013 at 08:53 AM 1
Share

Okay with another collider on the player and a rigibody on it it finally works. Thank you

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

20 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Trigger Enter and Exit not working properly? 2 Answers

Destroy On Collision will not work with spawned AI 1 Answer

Generating A Custom Collider For Multiple Meshes - 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