• 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
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?

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Instantiate a collider already colliding with an object not working 2 Answers

Collision detection accuracy issues. 1 Answer

Collission and Trigger on Same object 0 Answers

Temporarily ignore collisions between player and enemies 2 Answers

Collision vs Trigger [Solved] 5 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges