Search Unity

Elevator Script

Discussion in 'Scripting' started by tomazsaraiva, Oct 12, 2009.

  1. tomazsaraiva

    tomazsaraiva

    Joined:
    Mar 19, 2009
    Posts:
    22
    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:
    Code (csharp):
    1. private var triggerheight : int;
    2. private var elev_height : int;
    3. private var trigOn : boolean = false;
    4. private var speed = 1;
    5. var elevator : GameObject;
    6. var trigger: GameObject;
    7. var elevator_door: GameObject;
    8.  
    9. function OnTriggerEnter(other : Collider) {
    10.     trigOn = true;
    11. }
    12.  
    13. function OnTriggerExit(other : Collider) {
    14.     trigOn = false;
    15. }
    16.  
    17. function CallElevator()
    18. {
    19.     elev_height = elevator.transform.position.y;
    20.     triggerheight =trigger.transform.position.y;
    21.    
    22.     if(trigOn == true)
    23.     {
    24.         if(Input.GetKey("e"))
    25.         {
    26.             Mathf.RoundToInt(elev_height);
    27.             Mathf.RoundToInt(triggerheight);
    28.            
    29.             while(elev_height > triggerheight)
    30.             {
    31.                 elevator.transform.Translate(Vector3(0, 0, -speed* Time.deltaTime));
    32.                 yield;
    33.                
    34.                 if(elev_height == triggerheight)
    35.                 {
    36.                     elevator.animation.Play("open");
    37.                     elevator_door.animation.Play("open");
    38.                    
    39.                     yield new WaitForSeconds (5);
    40.                    
    41.                     elevator.animation.Play("close");
    42.                     elevator_door.animation.Play("close");
    43.                
    44.                 }
    45.             }
    46.            
    47.             while(elev_height < triggerheight)
    48.             {
    49.                 elevator.transform.Translate(Vector3(0, speed* Time.deltaTime, 0));
    50.                 yield;
    51.                
    52.                 if(elev_height == triggerheight)
    53.                 {
    54.                     elevator.animation.Play("open");
    55.                     elevator_door.animation.Play("open");
    56.                    
    57.                     yield new WaitForSeconds (5);
    58.                    
    59.                     elevator.animation.Play("close");
    60.                     elevator_door.animation.Play("close");
    61.                 }
    62.             }
    63.         }
    64.     }
    65. }
    66.  
    67. function Update ()
    68. {
    69.     CallElevator();
    70. }
     
  2. Heranor

    Heranor

    Joined:
    Jul 15, 2011
    Posts:
    3
    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!
     
  3. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    The Mathf.RoundToInt() lines don't do anything, Heranor is right to have them removed.
     
  4. lesenfantterribles

    lesenfantterribles

    Joined:
    Jul 23, 2011
    Posts:
    1
    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?