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 /
avatar image
1
Question by AmazingDeveloper2 · Feb 04, 2016 at 04:37 PM · c#animationunity 5

How to open the door when I need

I have a door and buttons on the both sides of the door, which open it.

When I press Button1, door opens. When I press it again, door closed. But if I will press Button1 (door opens), then walk into the room and press Button2, the door will opens again, but this time I need to close it.

alt text

I open/close using animation. Can't understand, what is wrong

 public Animation button1;
 public Animation button2;
 public Animation gate;
 
 public AudioSource myAudio;

 private bool isDoorClosed;
 private bool isAimationReadyToPlay = true;
 private Collider thisCollider;

 public void Start()
 {
     thisCollider = GetComponent<Collider>();
 }

 void Update()
 {
     if (Input.GetButton("Fire1"))
         if (DoPlayerLookAtButton() && isAimationReadyToPlay)
             OpenCloseDoor();
 }

 bool DoPlayerLookAtButton()
 {
     RaycastHit _hit;
     Ray _ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
     bool isHit = Physics.Raycast(_ray, out _hit, 1.5f);

     if (isHit && _hit.collider == thisCollider) return true;
     else return false;
 }

 void OpenCloseDoor()
 {
     myAudio.Play();
     StartCoroutine("DelayBetweenAnimations");

     if (!isDoorClosed) // Play animation with normal speed
     {
         button1["pressDoorButton"].speed = 1.0f;
         button1.Play();

         button2["pressDoorButton"].speed = 1.0f;
         button2.Play();

         gate["openDoor"].speed = 1.0f;
         gate.Play();
     }

     if (isDoorClosed) // Play animation with revert speed
     {
         button1["pressDoorButton"].speed = -1.0f;
         button1["pressDoorButton"].time = button1["pressDoorButton"].length;
         button1.Play();

         button2["pressDoorButton"].speed = -1.0f;
         button2["pressDoorButton"].time = button2["pressDoorButton"].length;
         button2.Play();

         gate["openDoor"].speed = -1.0f;
         gate["openDoor"].time = gate["openDoor"].length;
         gate.Play();
     }

     isDoorClosed = !isDoorClosed;
 }

 IEnumerator DelayBetweenAnimations()
 {
     isAimationReadyToPlay = false;
     yield return new WaitForSeconds(0.5f);
     isAimationReadyToPlay = true;
 }

6546.jpg (28.2 kB)
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
1
Best Answer

Answer by BigHandInSky · Feb 04, 2016 at 05:13 PM

It is most likely because you haven't defined isDoorClosed in your variable declaration/Start(), and also because you aren't forcing the door to start from 0.0f time - unless specified with a "bool = " all bools are defined as false, which I think may be leading to your script triggering the wrong way around, so either use:

   private bool isDoorClosed = true;

or use:

  public void Start()
  {
      thisCollider = GetComponent<Collider>();
      IsDoorClosed = true;
  }

Alongside this in OpenCloseDoor():

          gate["openDoor"].speed = 1.0f;
          gate["openDoor"].time = 0.0f;
          gate.Play();

That may be the fix for it.

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 AmazingDeveloper2 · Feb 04, 2016 at 06:31 PM 0
Share

Time should be zero wen I play it in forward direction (open door). But when I close door, time must be equals to the length of animation and speed = -1. And I should call variable isDoorOpen, but with name isDoorClosed it works fine.

The problem is when I open door, variable isDoorClosed change it's value, but only for Button1 because of it: if (isHit && _hit.collider == thisCollider).So for Button2 door is still closed and for Button1 it is opened. I don't know, how to change it for both buttons

avatar image BigHandInSky AmazingDeveloper2 · Feb 05, 2016 at 11:34 AM 1
Share

I think I see the problem - you have the script on both buttons, but when you update one, the other is still in the starting state (I was reading it as a general script on the door itself), what you can do is one of the following:

Have a reference to another DoorScript, when the door is opened update the other script:

 public DoorScript OtherDoorButton;
 
 ...
 
 public void UpdateDoorState(bool newState)
 {
      isDoorClosed = newState;
 }

 ...

 isDoorClosed = !isDoorClosed;
 OtherDoorScript.UpdateDoorState(isDoorClosed);

Or, what I would recommend, is have the script be just one on the door itself, rather than a script per button. Then have a collider variable for both buttons, keeping your detection but checking whether the ray has hit either button's collider then update.

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

100 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

Related Questions

Door with two sliding parts problem 0 Answers

How do I get the fall animation to only play when the player is falling and then go back into an Idle when player is grounded again? 1 Answer

Cant get Animator Controller to work properly 2 Answers

how do I set the Animations For running and walking properly to match the stamina bar? 1 Answer

How to use one script for two objects? [Door opening] 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