# Elevator Script (399498)

**URL:** https://discussions.unity.com/t/elevator-script-399498/399498
**Category:** Scripting
**Tags:** Scripting
**Created:** [October 12, 2009, 11:54am UTC](https://discussions.unity.com/t/elevator-script-399498/399498 "2009-10-12T11:54:55Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![tomazsaraiva](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/tomazsaraiva/32/316769_2.png) [@tomazsaraiva](https://discussions.unity.com/u/tomazsaraiva)
#### Post date: [October 12, 2009, 11:54am UTC](https://discussions.unity.com/t/elevator-script-399498/399498/1 "2009-10-12T11:54:55Z")

</div>

Hey,

I’m trying to create an elevator. I’ve finally managed to create a script that allows the activate the elevator calling it to the floor where the player is.

The problem is that when the elevator reaches the floor I can’t align it correctly. I tried to round the float values to integer but it didn’t work

Here is my code:

```auto
private var triggerheight : int; 
private var elev_height : int; 
private var trigOn : boolean = false; 
private var speed = 1; 
var elevator : GameObject; 
var trigger: GameObject; 
var elevator_door: GameObject; 

function OnTriggerEnter(other : Collider) { 
	trigOn = true; 
} 

function OnTriggerExit(other : Collider) { 
	trigOn = false; 
} 

function CallElevator() 
{ 
	elev_height = elevator.transform.position.y; 
	triggerheight =trigger.transform.position.y; 
	
	if(trigOn == true) 
	{ 
		if(Input.GetKey("e")) 
		{ 
			Mathf.RoundToInt(elev_height); 
			Mathf.RoundToInt(triggerheight); 
			
			while(elev_height > triggerheight) 
			{ 
				elevator.transform.Translate(Vector3(0, 0, -speed* Time.deltaTime)); 
				yield; 
				
				if(elev_height == triggerheight) 
				{ 
					elevator.animation.Play("open"); 
					elevator_door.animation.Play("open"); 
					
					yield new WaitForSeconds (5); 
					
					elevator.animation.Play("close"); 
					elevator_door.animation.Play("close"); 
				
				} 
			} 
			
			while(elev_height < triggerheight) 
			{ 
				elevator.transform.Translate(Vector3(0, speed* Time.deltaTime, 0)); 
				yield; 
				
				if(elev_height == triggerheight) 
				{ 
					elevator.animation.Play("open"); 
					elevator_door.animation.Play("open"); 
					
					yield new WaitForSeconds (5); 
					
					elevator.animation.Play("close"); 
					elevator_door.animation.Play("close"); 
				} 
			} 
		} 
	} 
} 

function Update () 
{ 
	CallElevator(); 
}

```

---

<div class="post-metadata">

### Author: ![anon\_8574512](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@anon\_8574512](https://discussions.unity.com/u/anon_8574512)
#### Post date: [July 15, 2011, 1:55pm UTC](https://discussions.unity.com/t/elevator-script-399498/399498/2 "2011-07-15T13:55:05Z")

</div>

First, change the vars “triggerheight” and “elev\_height” to be float-s, instead of int-s, since some times the height of an object is not a round number. For the same reason, and since the var-s are no longer int-s, you should remove this two lines:  
Mathf.RoundToInt(elev\_height);  
Mathf.RoundToInt(triggerheight);

It worked for me…

Best Regards!

---

<div class="post-metadata">

### Author: ![JRavey](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@JRavey](https://discussions.unity.com/u/JRavey)
#### Post date: [July 15, 2011, 2:20pm UTC](https://discussions.unity.com/t/elevator-script-399498/399498/3 "2011-07-15T14:20:34Z")

</div>

The Mathf.RoundToInt() lines don’t do anything, Heranor is right to have them removed.

---

<div class="post-metadata">

### Author: ![anon\_80163491](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@anon\_80163491](https://discussions.unity.com/u/anon_80163491)
#### Post date: [July 25, 2011, 1:55am UTC](https://discussions.unity.com/t/elevator-script-399498/399498/4 "2011-07-25T01:55:28Z")

</div>

hi! im making a texturing example environment and want a working elevator in it but cant script to save my life. i would love to use this script if thats ok. how does it work? which objects do i assign it to?
