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 zerophase · Nov 09, 2013 at 01:52 AM · c#animationprefabdoor

Flipping bool on one instance of prefab in scene

I'm detecting if a player is by a door, by flipping a bool in OnTriggerEnter. What I have works for animating the correct door opening, but when I try to parent the player with the door, I noticed the bool rapidly flips from true to false. So, the player is constantly being added and removed from the parent, with the end result of the door passing through him. How may I go about only flipping the bool to true for the door the player is near?

My Code

 void Update()
 {
      if(!this.flagDoor)
      {
           player.transform.parent = null;
      }
 }

 void OnTriggerEnter(Collider other)
     {
         if(other.gameObject.name == "Player")
         {
             this.flagDoor = true;
         }
     }
 

 void OnTriggerStay(Collider other)
     {
         if(this.flagDoor)
         {
             player.transform.parent = gameObject.transform.root;
         }
     }
     void OnTriggerExit(Collider other)
     {
         if (other.gameObject.name == "Player") 
         {
             this.flagDoor = false;
             //parentToObject = 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

1 Reply

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

Answer by Starwalker · Nov 10, 2013 at 09:42 AM

The problem is more design related here, adding the player to the door seems un-needed, why?, you need to explain for a full accurate answer.

The reason the door passes through him is because, when you parent an object to another, the child loses its own position and in-herits its parents position.

You'll need to keep the door and the player separate, you already have a trigger setup, you can do whatever you want in the OnTriggerEnter and OnTriggerExit , because these are events which fire /frame, when the trigger is touched. So you are parenting the player every frame, not just once, which is un-needed and inefficient.

If your purpose for all this is just to detect if the player is nearby the door, just increase the size of the collider, the collider can be made bigger without scaling the object itself.

If you can do the above, it would solve your problem, if not, redesign your code structure.

You can interect with other scripts using GetComponent or GameObject.Find, parenting is not required.

Comment
Add comment · Show 15 · 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 zerophase · Nov 10, 2013 at 12:19 PM 0
Share

I tried using rigid bodies at first, so the door would collide with the player; (he has a character controller) but, the door still pass through him. Even after playing around with is kinematic the door animation kept passing through the character. I ended up setting up a trigger box on each side of the door that are responsible for calling the appropriate door animations, and parenting the player to the door, If he's in the way of the animation. They also detach the player, if he leaves the trigger or the current animation wouldn't collide with him.

avatar image Starwalker · Nov 10, 2013 at 06:58 PM 0
Share

Doesn't your character have a collider on him? What type of collider is this? Usually when to collider's intersect, the 1st one will push the other out.

avatar image zerophase · Nov 10, 2013 at 08:48 PM 0
Share

He has a capsule collider.

avatar image Starwalker · Nov 11, 2013 at 12:18 AM 0
Share

Doesnt the door itself has a box or mesh collider on it?

avatar image zerophase · Nov 11, 2013 at 08:14 AM 0
Share

Yeah, the door has a box collider and rigid body.

Show more comments

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

18 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

Related Questions

How to wait for animation completion (C#)? 2 Answers

Multiple Cars not working 1 Answer

creating animation made my each prefab spawn at same position 1 Answer

Making an animated door, that will be reused over a level, need to reference its parent 2 Answers

Open Door on Keypress Not Working - Animations 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