- Home /
Door is glitching open and close
Needing a door to open and close, I hunted for a tutorial as to how it is done. I came across this video: https://www.youtube.com/watch?v=tUbNIwn1Htc, and started on my door. At first, the door would just open when the gameplay started, but the code initially had a void start
so I changed it to void update
. It fixed the door opening on its own problem, but when I get close to the door and stand in the door frame, the door will glitch open and close (it is supposed to stay open when the character is in the box collider, and then close when the character is not in it). Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorScript : MonoBehaviour {
Animator anim;
// Use this for initialization
void Update () {
anim = GetComponent<Animator>();
}
void OnTriggerEnter(Collider other)
{
anim.SetTrigger("OpenDoor");
}
void OnTriggerExit(Collider other)
{
anim.enabled = true;
}
void pauseAnimationEvent()
{
anim.enabled = false;
}
}
This is almost the exact code from the video (except for the first method) and it worked for the guy in the tutorial (my friend thinks that I need another animation for CloseDoor as well as OpenDoor, yet they are the same thing and the animation is supposed to pause at the animation event until the character exits the collider). Let me know if you need any more information that would help you with figuring out the solution.
EDIT: @highpockets It is still glitching out. I copied the code that you sent, it does not open right on pressing play, so that is good, but it isn't closing when I go through the door. It is staying open. Here is a picture of the door open when it should not be: https://funkyimg.com/i/2T6Wx.png. Also, you said that "if it keeps opening and closing after you go on the trigger and leave the first time, it is likely because it is the default state in your animator layer" but here is an image of my animation: https://funkyimg.com/i/2T6X8.png, so that is not an issue, since the default layer is on "DoNothing" and not on "DoorAnimation".
Answer by highpockets · Apr 11, 2019 at 09:49 PM
I think it should look like my code below. You have a method called pauseAnimationEvent() that isn’t being called in the script, but I think you should have an open door and close door animation and do what I have done. You can quite easily turn the animation clip into an additional one that is a reversed of the original by adding a second of the same clip in the import settings and then put it in your animator and click on the state and tick mirror. And setting the animator in the Start method is the right call because otherwise you are just setting the variable to the same thing every frame.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorScript : MonoBehaviour {
Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
anim.enabled = false;
}
void OnTriggerEnter(Collider other)
{
anim.SetTrigger("OpenDoor");
}
void OnTriggerExit(Collider other)
{
anim.enabled = true;
}
public void pauseAnimationEvent()
{
anim.enabled = false;
}
@highpockets the pauseAnimationEvent is in an event in the animation at the point where the door is fully opened. It makes it so that the animation is false, so it effectively pauses itself, but it is not really working since it is continuing to open and close. $$anonymous$$aking an entire other animation is a lot of work, do you think that is the best course of action for me? Also, how does your script work with void Start()? That always opened the door when I clicked play.
Ok, if your animation starts at the beginning of the game and you haven’t entered the trigger in question, it is likely your default state, so set anim.enable to false in the start method. You don’t need anything in the update method of your script. If you have an event, sorry, I will redo my above code now to reflect how I think it should workout
Ok I changed my code above. If it keeps opening and closing after you go on the trigger and leave the first time, it is likely because it is the default state in your animator layer. You need to change that, you can make an empty state the default and make a transition to your opening door animation when you need to open and back to the empty state again when it is finished. You’ll have to ensure that the door animation is not set to loop as well
@highpockets I will get back to you soon on if it works, I am going on Spring Break in like 3 days, so I will not have a lot of time to implement this, but I will definitely give it a try. Thank in advance, I am sure this will work since I did change the default state in my animation to a blank state.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Pausing Door Corountine During Collision 0 Answers
automatic door with coroutine 2 Answers
Opening door with the same key? 1 Answer