Help With Door Open Script

I have a script that I am using:

var doorOpened : boolean = false;
var doorAudio : AudioClip;
var doorShut : AudioClip;
var timer : float = 0.0;

function OnControllerColliderHit(hit:ControllerColliderHit){

if((hit.gameObject.tag == “house1door”) (doorOpened == false)){
openDoor();
}

}

function Update(){

if(doorOpened){
timer += Time.deltaTime;
}

if(timer >= 2){
shutDoor();
}
}

function shutDoor(){

var theHouse = gameObject.FindWithTag(“house1”);
theHouse.animation.Play(“doorshuts”);
doorOpened = false;
audio.PlayOneShot(doorShut);
timer = 0;
}

function openDoor(){

doorOpened = true;
var theHouse = gameObject.FindWithTag(“house1”);
theHouse.animation.Play(“dooropen”);
audio.PlayOneShot(doorAudio);

}

and I have gotten it to work, but there are two problems that I am having:

1 - The initial animation of the doors themselves shows the door slightly ajar, even though the original animation in Cinema 4d is NOT open. This is how I have it set up:

frames 1-10 are idle
frames 11-21 are it opening
frames 21-31 are of it closing

In C4d it plays just, and what happens is that when I walk up to the door it pays fine and closes fine, it closes completely, but its initial state is slightly open. Am I missing something?

Also, when I try to make this door a prefab, the original first door stops working but still affects the opening and closing of any new doors I make, but only one door will ever work at a time. (EDIT) I have solved this problem by checking the LOOP tick for the idle frames 1-10

Any ideas anyone?

Heh! I just made a non-animated object and rotated it around an empty object. Unless it’s doing something fancy, you’re usually better off rotating it in-engine. If you want to get fancy, you could give it a rigidbody and turn on rigidbody.angularVelocity.y when it opens and closes. That takes a little more coding, though.

Sci-Fi doors are much easier. You can just have it go sideways, upwards, or downwards until it reaches a certain spot.

I had problems with animation performance in Cinema 4D. Sometimes I solve this problems putting objects inside null objects.

Make sure you use linear animations. Assign the idle animation to 1-9 frames or perhaps 1-5 and see what happens.

I spent most of Sunday resolving this very issue. I wanted a door that acted like the door in the ShadowDemoProject, but I wanted to control opening and closing on mouse clicks.

Here is what I did to make it work perfectly.

I started with my door mesh adding a rigidbody, box collider and a configurable joint.

The settings on the configurable joint are very important, but you have to control the targetAngularVelocity in script in order to get your opening and closing. You also need the box collider to detect mouse events.

The main areas of the configurable joint to setup are the Axis(0,1,0) and then setting x,y,z motion to Locked, and Angular XMotion to Limited, and Angular Y and ZMotion to Locked.

Then I set the Low Angular Limit to -90 and the High Angular Limit to 0, and then the targetAngularVelocity to a positive speed(1.0 is a good start), and finally the Maximum Force of the Angular XDrive to 0.5 and this opens the door in an inward direction, 90 degrees. I simply change the targetAngularVelocity to a negative speed, and this closes the door back 90 degrees to its starting position.

Here is the code:

// Doors

var doorOpenCloseSpeed = 1; // our exposed door speed

private var doorJoint : ConfigurableJoint; // handle to our component
private var doorSpeed = 0; // stores our door speed without changing it
private var doorOpenClose = true; // false=closed, true=open
private var onMouseDownFlag = false; // keeps a speed from being called repeatedly

function Start()
{
	doorJoint = GetComponent(ConfigurableJoint);
	if(!doorJoint) { Debug.Log("No handle to door joint"); }
		
	doorJoint.targetAngularVelocity = Vector3.zero; // initialize to no movement
}

function OnMouseDown()
{
	doorOpenClose = (doorOpenClose == false);
	onMouseDownFlag = true;
}

function LateUpdate()
{
	if(onMouseDownFlag == true)
	{
		onMouseDownFlag = false;
		doorSpeed = doorOpenCloseSpeed;
		if(doorOpenClose == false) 
		{ 
			doorJoint.targetAngularVelocity = Vector3(doorSpeed,0,0);
		}
		else 
		{ 
			doorJoint.targetAngularVelocity = Vector3(-doorSpeed,0,0);
		}
	}
}

Hope that helps someone out :slight_smile:

-rightpurdy

I’m kind of with Mr. Sunman here.

Even if you have to spend some time tweaking the animation (to make it jerk a bit at the start, etc.) once you have a robust door script it will be a gift that keeps on giving. (Imagine you can load a scene and everything named “door” just works…)