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 Fanttum · May 29, 2014 at 01:03 AM · animationdoorcolideropening

Open door as Character approaches

So I can not for the life of me figure out how to make a door move out of the way when I am walking up to it.

I watched this as a base but I think it is kinda throwing me off. I got the animations to sorta work but they the door is just moving up and down all the time. Maybe i should just make this more simple and uses forces and stuff instead of animations but this should be the easier way if I can just figure it out.

My set up right now it as fallows.

A door trigger with my script, rigid body and animation attached. Then a door (cube) with its mesh intact parented to the trigger.

Code I have used: (in c#)

 using UnityEngine;
 using System.Collections;
 
     public class DoorOpener : MonoBehaviour {
     
     private HashIDs hash;      
     private Animator anim;
     private GameObject player;
     private int count;  
  
     void Awake ()
     {
         // Setting up the references.
         anim = GetComponent<Animator>();
         hash = GameObject.FindGameObjectWithTag("GameController").GetComponent<HashIDs>();
         player = GameObject.FindGameObjectWithTag("Player");
     }
     void OnTriggerEnter(Collider other) 
     {
                 if (other.gameObject == player) {
                         count++;
             Debug.Log ("OPEN");
                 }
         }
         void OnTriggerExit (Collider other)
         {
             if(other.gameObject == player) {
                 count = Mathf.Max(0, count-1);
             Debug.Log ("CLOSE");
             }
         }
         void Update ()
         {
             anim.SetBool(hash.openBool,count > 0);
         }
 }

Also a hashID script to go along with that, Sorry for any sloppy coding syntax

Any ideas? thanks, kindling bugging me that I can't figure this out

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
0

Answer by meat5000 · May 29, 2014 at 01:05 AM

The door uses Mecanim?

Seems a little much for a door, a simple rotate or move would be more efficient.

Did you add your parameters to the transitions?

If you have just Open and Close in your state machine, add a third Empty State and make it default. Provide your transitions from this empty.

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 Fanttum · May 29, 2014 at 02:00 AM 0
Share

That's what I was thinking would be a good solution but animations should have worked I would think. It is just moving up and down by the way, no rotations.

avatar image meat5000 ♦ · May 29, 2014 at 02:10 AM 0
Share

It simply sounds like the Anim states are cycling through from one to the next.

Using the default empty and providing it will a parameter in the statemachine will ensure this doesn't happen.

avatar image
0

Answer by Merrick · May 29, 2014 at 01:46 AM

I did this following a tutorial, is this what you are looking for? I'm incredibly noob at this coding stuff, and your code for me looks extremely confusing.

 {
 
     bool doorIsOpen = false;
     float doorTimer = 0.0f;
     public float doorOpenTime = 3.0f;
     public AudioClip doorOpenSound;
     public AudioClip doorShutSound;
 
     void Start(){
         doorTimer = 0.0f;
     }
     void Update(){
                 if (doorIsOpen) {
                         doorTimer += Time.deltaTime;
                         if (doorTimer > doorOpenTime) {
                                 Door (doorShutSound, false, "doorshut");
                                 doorTimer = 0.0f;
                         }
                 }
         }
 
     void DoorCheck(){
         if(!doorIsOpen){
             Door(doorOpenSound, true, "dooropen");
         }
     }
 
     void Door(AudioClip aClip, bool openCheck, string animName){
         audio.PlayOneShot(aClip);
         doorIsOpen = openCheck;
         transform.parent.gameObject.animation.Play(animName);
     }
 }


This code is pretty simple, I have 3 animations on the door, an IDLE an Open and Close animation. And the rest I think it's self explained, but if you have any doubt I'll walk you through...

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 meat5000 ♦ · May 29, 2014 at 01:48 AM 0
Share

It appears your code uses legacy animation. OPs code looks confusing to you as

 anim.SetBool(hash.openBool,count > 0);

Sets a parameter in the Animator State $$anonymous$$achine, where 'Animator' is a part of $$anonymous$$ecanim and is a different beast to 'Animation'.

avatar image Fanttum · May 29, 2014 at 02:03 AM 0
Share

@meat5000 Yea, that line of code is straight from the video. I understand it just not quite sure it's working right.

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

22 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

Related Questions

Door script not completely working help! 1 Answer

Trying to Open a Door 2 Answers

door opening 3 Answers

opening a door with animation 1 Answer

Sliding door animation question. 3 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