Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 cory · Feb 28, 2011 at 01:56 AM · animationcollidersdooropening

Trying to Open a Door

Ok, so I read through a bunch of the previous posted questions concerning this topic, but none of them really answered my question.. I'm extremely new to Unity and C#... What I am trying to do is open a door via an animation when the player approaches it. I have the doors already animated, and its under the door object when i expand it. How do I go about writing the right script, and setting up the colliders and what not that is needed? If anyone can help me you'd be a life saver.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by AngryOldMan · Feb 28, 2011 at 02:10 AM

may i suggest using java script instead? it's alot easier especially if your new to scripting or you already know actionscript. it may look something like this (assuming you want the door to open and stay open)

var door : GameObject; private var HasBeenTriggered : int;

HasBeenTriggered = 0;

function OnTriggerEnter (collision : Collider) { if (collision.gameObject.tag == "teaPot") { if(HasBeenTriggered == 0) { door.animation.Play (); HasBeenTriggered = 1; } } }

Comment
Add comment · Show 2 · 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 cory · Mar 01, 2011 at 12:43 AM 0
Share

Bob how would I go about setting up my different pieces to work with that script?

avatar image AngryOldMan · Mar 01, 2011 at 02:15 AM 0
Share

stick the script on an empty game object that has a collider with "is trigger" ticked. Change the bit that says "$$anonymous$$pot" to "Player" then tag your character Player in the inspector. $$anonymous$$ake sure your "door" has an animation on it with "play automatically" unchecked. Highlight the trigger object so it's properties appear in the inspector and drag the "door" gameobject into its script component,there will be a section that says "door Gameobject(none)" with all this set up you should just be able to move your character into the trigger area and it plays the door opening animation

avatar image
0

Answer by Bunny83 · Feb 28, 2011 at 04:27 PM

Here's a C# script that opens a door. Once it's triggered the script deletes itself. In my case the animation to open the door is named "open". Make sure the WrapMode of your animation is set to Once. That way the door stays open. This script have to be attached to a GameObject that act as trigger. Add a collider, eg. BoxCollider to this GameObject, set it's isTrigger property and place it in front of the door. You have to drag&drop your animated door onto the animatedDoor variable of the script.

In C# (and also in JS) it's the scriptfiles name that have to be equal to the classname. In JS you can omit the class header and JS will automatically inherit from MonoBehaviour.

using UnityEngine; using System.Collections;

public class OpenDoor : MonoBehaviour { public Animation animatedDoor = null; void Start() { if (animatedDoor == null) Destroy(this); // The script needs the variable to be set } void OnTriggerEnter(Collider other) { if (other.tag == "Player") { animatedDoor.Play("open"); Destroy(this); // when triggered remove this script } } }

If you need more complex doors (eg. that closes automatically and so on) that involves a bit more logic in the script. Make yourself familiar with the Unity-api. Unity is very well documented, so take a look at the scripting reference and the runtime classes to understand the concept of Unity.

Comment
Add comment · Show 4 · 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 cory · Mar 02, 2011 at 03:13 AM 0
Share

hey I'm sorry, but I typed up the code you gave me, and set up everything the way you said to. Only problem is that I don't have an animatedDoor vairable to drag and drop my door onto

avatar image Bunny83 · Mar 02, 2011 at 09:54 AM 0
Share

Sorry, had a mistake. In C# inside a class everything is private by default. just add public infront of the fariable. I've changed my answer. Good you've spotted it ;) I didn't tried the sample, just wrote it from scratch.

avatar image cory · Mar 03, 2011 at 02:31 AM 0
Share

Fantastic it worked.. You really helped me out big time I needed to get this work for one of my college game design classes. Which is also why I wanted to stick to C# because that is what they're $$anonymous$$ching us. I was wondering since you've been so helpful how would I go about making the camera shake, and have a dust particle effect fall from the ceiling?

avatar image Bunny83 · Mar 03, 2011 at 02:41 AM 0
Share

Well, everything is possible ;) For the particle effect all you need is a particle emitter. To make the particles look like dust you may need to use a particle texture. It could be a lot of try and error to get the right look and feel but it shouldn't be a big problem. Camera shake effects have been asked already just search for it. If you can't find a solution that suits your needs ask another question. But first look here: http://answers.unity3d.com/questions/19559/is-it-possible-to-shake-the-camera

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

No one has followed this question yet.

Related Questions

door opening 3 Answers

importing exporting Animation help. 1 Answer

Open door as Character approaches 2 Answers

collider animation problem 1 Answer

How to open individual prefab doors 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