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 PvTGreg · May 05, 2014 at 07:59 PM · animation

On enter door open

Hi im kinda confused on this im starting to learn csharp and am trying to make a door open when a key is pressed within the box colider trigger this is what i have so far but how do i get the trigger working so that when the is pressed inside the trigger

 using UnityEngine;
 using System.Collections;
 
 public class Door : MonoBehaviour {
 
     private int m_LastIndex;
     
     public void PlayDoorAnim () 
     {
         if(!animation.isPlaying)
         {
             if(m_LastIndex == 0)
             {
                 animation.Play("DoorOpen");
                 m_LastIndex = 1;
             }
             else 
             {
                 animation.Play("DoorClosing");
                 m_LastIndex = 0;
             }
         }
     }
 }
 
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

Answer by randomperson42 · May 06, 2014 at 06:33 AM

Add a script to your GameObject with the trigger box collider. Inside the script, add the OnTriggerEnter() function. This will be called when a collider enters the trigger area. So if the collider that has entered the trigger is the player, you want to call the PlayDoorAnim() function.

The following (untested) code shows how to do this assuming that your player has the tag "Player". It also assumes that your trigger GameObject has a collider attached with the "isTrigger" field set to true.

 using UnityEngine;
 using System.Collections;
 
 public class DoorTrigger : MonoBehavior
 {
     public GameObject door;
     
     void OnTriggerEnter(Collider col)
     {
         if (col.gameObject.tag == "Player")
         {
             door.GetComponent<Door>().PlayDoorAnim();
         }
     }
 }


Edit: You can also use OnTriggerExit() to detect the player leaving the trigger area. See the scripting reference: OnTriggerEnter

OnTriggerExit

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 PvTGreg · May 06, 2014 at 06:17 PM 0
Share

NullReferenceException: Object reference not set to an instance of an object dooropen.OnTriggerEnter (UnityEngine.Collider col) (at Assets/dooropen.cs:12)

avatar image PvTGreg · May 06, 2014 at 06:17 PM 0
Share

this also happens with the asset simple door open asset that i bought to try and get it to work

avatar image randomperson42 · May 06, 2014 at 07:27 PM 0
Share

You need to make sure that you set the door GameObject in the inspector. Inside Unity, you select the GameObject that has the DoorTrigger (or equivalent) script, then simply drag-and-drop your door object onto the door variable slot.

avatar image PvTGreg · May 06, 2014 at 07:48 PM 0
Share

ive got my door ive put a box colider on it set as trigger and expanded it a bit is that what you mean?

avatar image randomperson42 · May 06, 2014 at 07:55 PM 0
Share

I see what you mean. I thought you had your collider trigger on a separate object. I meant for you to put the script above on a separate collider object with a reference to the door. This is probably what you want since a door animation could cause problems with changing the trigger area, if that makes sense.

So remove the above script from your door, and put it on a separate GameObject with a trigger collider. Then set the door variable to your door object.

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Mecanim, weapon bone animation woe... 1 Answer

AnimationEvent has no function name specified 2 Answers

Animated Object moving when clicked 1 Answer

I need help with adding my character controller to a Character model 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