Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by Joybutton · Nov 27, 2016 at 10:23 AM · rotationdoortroubleshooting

Button/Rotation script troubleshooting. Script working, but rotation is instant instead of timed.

Hello. I'm new to C# scripting and am working on a variation of a simple door/button script. I've managed to get it working as intended, but the door rotates instantly instead of smoothly transitioning. I'm hoping someone can help me troubleshoot the script.

The setup is a door object and button object. Each time the button script is activated the door object rotates 180 degrees. Right now it triggers, multiple times successfully, and snaps into the correct position each time but doesn't smoothly transition.

Button Script

 using UnityEngine;
 using System.Collections;
 public class TriggerButtonOld180 : MonoBehaviour
 {
     public OldDoor180 door;
 
     void OnTriggerEnter(Collider collider)
     {
         if (collider.gameObject.tag == "Player")
         {
             StartCoroutine(door.OpenUp());
         }
     }
 }

DoorScript

 using UnityEngine;
 using System.Collections;
 public class OldDoor180 : MonoBehaviour
 {
     public float smooth = 1.0f;
 
     private Vector3 OpenRotation;
 
 
 
     public IEnumerator OpenUp()
     {   
             {
                 OpenRotation = transform.eulerAngles + 180f * Vector3.up;
 
                 transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, OpenRotation, smooth * Time.deltaTime);
 
                 if (Vector3.Angle(transform.eulerAngles, OpenRotation) <= 0.25f){
                     transform.eulerAngles = OpenRotation;
                 
                 }
 
                 yield return null;
             }
         }
     }

Any help is appreciated. This is working as a placeholder, but I'd really like to get a nice transition working if possible.

( @MattSmiechowski - This is building on what you helped me with earlier. I was able to get that previous script working, thank you again. For this version I needed the object to rotate 180deg each time triggered instead of rotating to a set angle. Just a note in case you are out there and see this!)

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 alexanderameye · Nov 27, 2016 at 12:00 PM

Try these, it works but it might not be the most efficient way.

 using UnityEngine;
  using System.Collections;
  public class TriggerButtonOld180 : MonoBehaviour
  {
      public GameObject door;
  
      void OnTriggerEnter(Collider collider)
      {
          if (collider.gameObject.tag == "Player")
          {
              StartCoroutine(door.GetComponent<OldDoor180>().OpenUp());
          }
      }
 
   void OnTriggerStay(Collider collider)
      {
          if (collider.gameObject.tag == "Player")
          {
              StartCoroutine(door.GetComponent<OldDoor180>().OpenUp());
          }
      }
  }


and

 using UnityEngine;
  using System.Collections;
  public class OldDoor180 : MonoBehaviour
  {
      public float smooth = 1.0f;
  
      private Vector3 OpenRotation;
  
  void Start()
 {
   OpenRotation = transform.eulerAngles + 180f * Vector3.up;
 }
  
      public IEnumerator OpenUp()
      {   
              {
                 
                  transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, OpenRotation, smooth * Time.deltaTime);
 
                  yield return null;
              }
          }
      }


For this to work I attached the TriggerButtonOld180.cs script to an empty gameobject in my scene that had a collider attached to it that functions as a trigger. Then I had a door named 'OldDoor180' in my scene with the OldDoor180.cs script attached to it and I clicked and dragged that door gameobject in to the gameobject field in the TriggerButtonOld180 script.

Ps: I created this easy script that helps you to create openable doors, you might want to check it out :)

https://www.assetstore.unity3d.com/en/#!/content/38694

Comment
Add comment · Show 1 · 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 Joybutton · Nov 28, 2016 at 01:08 AM 0
Share

Hi @alexanderameye,

I tried this script and I get an interesting effect. Each time I hit the button the door rotates one 'tick' ins$$anonymous$$d of rotating 180deg. If I hit the button multiple times it slowly progresses.

The problem I'm running into is I need to just hit the button once, then have it smoothly transition 180deg. $$anonymous$$y scene setup is similar, except the button is attached to the rotating door object. Because the button moves along with the door there's no way to 'hold' the button down which I think is what your script would do.

It's still very useful and I also plan to look at your script on the asset store. Do you know how to make the script work with a single touch of the button? That's the reason why the original button script only had OnTriggerEnter.

Your help is greatly appreciated!

Edit:

I did a second test with your script by moving the button off the rotating door. The door does rotate 180deg while the button is pressed, but once it rotates 180deg the button no longer works. In the original script hitting the same button repeatedly would rotate the door 180deg each time. Since this script is also useful, is there a way to make it so that after the door has been rotated 180deg, pressing and holding the button will rotate it 180deg again? That would make it close enough to what I need to work if the other way isn't possible.

Thank you again for all your time!

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

91 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

Related Questions

3D top down player rotation error 2 Answers

How do I limit a door's rotation? 0 Answers

Rotate an object when two others collide? 1 Answer

Raycast Trouble 0 Answers

Checking rotation of door 0 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