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!)

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 :slight_smile: