Elevator Script (399498)

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:

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(); 
}

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!

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

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?