Opening door via script

Hi,
I would like to control opening door via script. Set a speed,angle etc.
So I started with

transform.Rotate (0, 0,90);

How to control a time of opening?
rotatevalue = 90*(2 * Time.deltaTime)
Yes… it doesn’t do what I need.
I am not sure with the Time.deltaTime.
Thanks for any help.

first this will help your object not rotate superfast per frame

transform.Rotate(0,speed * Time.deltaTime, 0, Space.Self);

an let’ it rotate at it’s own pivot Space.Self
otherwise it would rotate on world axis Space.World

set a variable for speed like this

var speed : float = 8.0;

This might interest you: It’s a door wich slides from one point to another an closes after some time.

// Animates the position to move from start to end within one second
var seconds : float = 0.5;
var openfor : float = 3;
var start : Transform;
var end : Transform;

private var isOpening:boolean = false;
private var isClosing:boolean = false;
private var counter:float = 0.0;

private var isOpen:boolean = false;

function Update()
{
	if (isOpening)
	{
		counter += Time.deltaTime;
		transform.position = Vector3.Lerp(start.position, end.position, counter * seconds);
	}
	else if (isClosing)
	{
		counter += Time.deltaTime;
		transform.position = Vector3.Lerp(end.position, start.position, counter * seconds);
	}
}

function OpenDoor () {
	
	counter = 0.0;
	isOpening = true;
	yield WaitForSeconds (seconds);
	isOpening = false;

	isOpen = true;
	
	yield WaitForSeconds (openfor - seconds);

	
	counter = 0.0;
	isClosing = true;
	yield WaitForSeconds (seconds);
	isClosing = false;
	
	isOpen = false;
}

put this script on the door, place two empty gameobjects for strt and end, grag them in the scripts variable slots for start and end

Put this script on a smal cube it will be the trigger button for the door to open when you touch it.

var door : Transform;

function OnTriggerEnter () {
	if (door.GetComponent("move from to lerp") != null)			
		door.GetComponent("move from to lerp").OpenDoor();	
   }

drag the door in the slot door

I have gotten some help on this one. It works but there is shurely a more eficient way. But maybe it gets you some idea. Primarily it’s this the Button calls a function in a script which the target object, the door has. This script moves the door from A to B and back.

Thanks.
I have still some problems.
I do not know how and where call
transform.Rotate for open door.
Definitely not at Update();

Yes transform.rotate happens in a functio Update.
Because it’s a updating movement.

Test this script (attach to a cube)

function Update () {

	if (Input.GetKey ("up"))
	{
		transform.Rotate(Time.deltaTime * 12, 0, 0, Space.World);
		
	}
}

IF UP is pressed the “door” rotates

There a guy at http://www.learnmesilly.com he has some free video tutorials which may answer your questions like doors, pickups and stuff.

Basic part of the script has been done!

function Start(){
t = DoorOpenAngel / speed;
yield WaitForSeconds(t);
opened = true;
}
if(opened== false){

transform.Rotate(0, 0, Time.deltaTime * speed, Space.Self); 
}

But I have another problem. Player can go through the door during opening however I have box collider on the door and character controller on my player.

Would you post your complete script. I can’t figure it out from those two parts :slight_smile:

script from the beginning with the variables

var ect.

to the end
}

pls.

var lock: boolean= false;
var TargetsToTrigger : Collider[];
var DoorOpenAngle = 90.0;
var speed = 180.0;
private var opened = true;
private var t = 0.0;
private var v = 0.0;

function Update () {

if(opened== false){

transform.Rotate(0, 0, Time.deltaTime * v, Space.Self); 
}

}

function Open(){
opened = false;
t = DoorOpenAngle /speed;

v = DoorOpenAngle /t;

yield WaitForSeconds(t);
opened = true;
}

function OnTriggerEnter (collision_info : Collider) {
if (collision_info == TargetsToTrigger[0]){
if(lock==false){
Open();

  }
                                                      }
}