Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Dec 24, 2016 at 05:25 PM by Prototype-Industrie for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Prototype-Industrie · Dec 13, 2016 at 11:27 AM · c#2dtrigger

Exit from OnTriggerEnter doesn't work.

Hello,

I've a problem, when I enter in the trigger everything works fine but when I exit from this my character continues to be like if he's in it. I've tried with OnTriggerExit and with OnTriggerStay but it's the same. Can you help me please ? :)

Here is my code :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Child : MonoBehaviour 
 {
 
 [SerializeField]
 Transform platform;
 
     void Start ()
     {
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if(other.tag == "Player")
         {
             other.transform.parent = platform.transform;
         }
     }
 }
Comment
Add comment · Show 5
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 JeffHardddyyy · Dec 13, 2016 at 02:27 PM 0
Share

In the script you have provided, there is no "OnTriggerExit".

avatar image Prototype-Industrie JeffHardddyyy · Dec 13, 2016 at 02:45 PM 0
Share

I've made some researches and my code is good now (i think) :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Child : $$anonymous$$onoBehaviour 
 {
 
 [SerializeField]
 GameObject platform;
 
     void Start ()
     {
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if(other.tag == "Player" && other.tag == "Dynamic" )
         {
             other.transform.parent = platform.transform;
         }
     }
     
     void OnTriggerExit2D(Collider2D other)
     {
         if(other.tag == "Player" && other.tag == "Dynamic")
         {
             other.transform.parent = null;
         }
     }
 }

but i have an other problem. I've changed my character controller code and now nothing happen, even with the OnTriggerEnter. This is veeeeery annoying...

avatar image sumitb_mdi Prototype-Industrie · Dec 13, 2016 at 02:50 PM 0
Share

Copy paste your that code too. And make sure you have attached the Trigger colliders on those bodies.

Show more comments
Show more comments

1 Reply

  • Sort: 
avatar image
0

Answer by Prototype-Industrie · Dec 13, 2016 at 05:11 PM

@Landern

Here is the code i have now :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Child : MonoBehaviour 
 {
 
 [SerializeField]
 GameObject platform;
 
     void Start ()
     {
     }
 
     void OnTriggerStay2D(Collider2D other)
     {
         if(other.tag == "Player")
         {
             other.transform.parent = platform.transform;
         }
         
         if(other.tag == "Dynamic")
         {
             other.transform.parent = platform.transform;
         }
     }
     
     void OnTriggerExit2D(Collider2D other)
     {
         if(other.tag == "Player")
         {
             other.transform.parent = null;
         }
         
         if(other.tag == "Dynamic")
         {
             other.transform.parent = null;
         }
     }
 }

It works well with both "Dynamyc" and "Player objects but sometimes my character still get in the trigger when it should not. i don't know how to correct it. It does not happen often but it is annoying when it happens.

Comment
Add comment · Show 6 · 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 UnityCoach · Dec 13, 2016 at 08:18 PM 0
Share

OnTriggerStay2D is called every frame while the object is within a trigger collider area. You may want to consider using OnTriggerEnter2D ins$$anonymous$$d.

avatar image Prototype-Industrie UnityCoach · Dec 14, 2016 at 09:47 AM 0
Share

This is what I used first but it's worse, it's not working once in two: /

avatar image UnityCoach Prototype-Industrie · Dec 15, 2016 at 09:26 AM 1
Share

couple of things, may not be the problem, but it seems you never assign the platform transform variable from the code, you may want to just declare it public ins$$anonymous$$d of using the serialisable attribute, unless you really don't want the other classes to access it, and you may want to use "else if" or a "||" comparison, as other.tag will never be both "Player" and "Dynamic".

 if(other.tag == "Player" || other.tag == "Dynamic")
          {
              other.transform.parent = null;
          }
Show more comments
Show more comments

Follow this Question

Answers Answers and Comments

302 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 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

Detect the objects staying on top of the Particles 0 Answers

Setting up a function for sorting specific triggers entered based on a height integer. 0 Answers

How to move an object on a moving platform with the platform 0 Answers

LookAt in 2d is not working Again :/ 0 Answers

Help me with gui please (C#) 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