- Home /
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
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.
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.
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.
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...
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'.
@meat5000 Yea, that line of code is straight from the video. I understand it just not quite sure it's working right.
Your answer
Follow this Question
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