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 brookeblood1 · Feb 22, 2013 at 10:30 PM · collisioncollidertriggerfirst-person-controller

OnTriggerEnter Not Working

I am trying to use the following code to execute an action when my player collides with a certain object, but I can't seem to get it to work. I have the collider on the object the player should collide with as "Is Trigger", and the script is attached to the same object.

 function OnTriggerEnter(other : Collider)
 {
 
   if (other.name == "First Person Controller")
   {
     Debug.Log("collision");
   }
   
   }

EDIT: Got it working, I feel smart. :P Here is my finished code below for anyone who may need something like this.

 var target : GameObject;
 var inside : boolean;
 
 function OnTriggerEnter(other : Collider)
 {
 
   if (other.tag == "Player")
   {
         var ph : PlayerHealth = target.GetComponent("PlayerHealth");
         ph.AdjustCurrentHealth(-1);
         inside = true;
   }
   
   }
   
 function Update() {
 
 if(inside)
 {
     var ph : PlayerHealth = target.GetComponent("PlayerHealth");
     ph.AdjustCurrentHealth(-1);
 }
 
 if(!inside)
 {
 Debug.Log("Not colliding");
 }
 
 }
 
 function OnTriggerExit(other : Collider)
 {
 
   if (other.tag == "Player")
   {
         inside = 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

3 Replies

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

Answer by jmatthews · Feb 23, 2013 at 12:37 AM

Does the object the trigger is reacting to have a rigidbody?

is the player object moving? The physics engine puts rigid bodies to sleep when they're not moving and you'll get no collision detection. Try walking the player object into the laser beam. If you get your event to fire then this is your issue.

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 brookeblood1 · Feb 23, 2013 at 12:39 AM 0
Share

I just added one but it had no effect

avatar image brookeblood1 · Feb 23, 2013 at 05:02 PM 0
Share

Somehow it seems to be working now, but I still have another small issue. When the player collides the first time, it executes the action perfectly, but I need it to continue to execute the action while the player remains inside of the object.

avatar image jmatthews · Feb 23, 2013 at 06:36 PM 0
Share

Looks like you figured the OnExit out on your own, congrats. Please accept whichever answer helped you so this gets out of the unanswered stack.

avatar image
1

Answer by TheCheese · Feb 23, 2013 at 01:11 AM

Triggers require at least one of the objects involved to have a Rigidbody component. Check out the collision matrix at the bottom of this page for more info:

http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html

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 vincysq96 · Feb 23, 2013 at 12:57 AM

Try using:

if (other.tag== "Player")

and assign a tag "Player" to the First Person Controller

Comment
Add comment · Show 1 · 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 brookeblood1 · Feb 23, 2013 at 02:55 AM 0
Share

I changed the script to this, and accidentally had the collider checked off for the other object. When I turned it on, the collision just crashes unity and I have to end it with the task manager. Essentially, I want the player to be able to walk through to object (it is a laser beam) and take damage when he touches it. I already have the code to lower his health, but how should I handle the collision?

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

12 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

Related Questions

Colliding two GameObjects 1 Answer

Compound collider does not trigger the parent 1 Answer

Trigger Spawning? 1 Answer

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

Collision with renderer.enabled? 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