Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Simple Door (C#)

Discussion in 'Scripting' started by Guns are Toys, Nov 18, 2010.

  1. Guns are Toys

    Guns are Toys

    Joined:
    May 23, 2010
    Posts:
    21
    Hi. I'm currantly working on a very simple door for my game, I want it to just slide out of the way (in this case down) when the player aproaches it. So far I got it to half work. When the player makes contact with the door the door opens, but it dosn't stop when I want it to stop, it just keeps going. Any help would be greatly apreateated.

    Currant script:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Door : MonoBehaviour {
    5.    
    6.     public float speed = -1.0F;
    7.     public float Cspeed = 1.0F;
    8.     public Transform door;
    9.     public Transform open;
    10.     public Transform closed;
    11.     bool closing = false;
    12.     bool opening = false;
    13.    
    14.     void OnTriggerEnter (Collider target)
    15.     {
    16.         opening = true;
    17.     }
    18.    
    19.     void Update ()
    20.     {
    21.         Vector3 DoorOpen = new Vector3(0, open.transform.position.y, 0);
    22.         Vector3 DoorClosed = new Vector3(0, closed.transform.position.y, 0);
    23.         Vector3 DoorMoving = new Vector3(0, door.transform.position.y, 0);
    24.        
    25.         if (opening == true)
    26.             transform.Translate(0, speed*Time.deltaTime, 0);
    27.        
    28.         if (DoorMoving == DoorOpen)
    29.         {
    30.             opening = false;
    31.             closing = true;
    32.             transform.Translate(0, 0, 0);
    33.         }
    34.        
    35.         if (closing == true)
    36.             transform.Translate(0, Cspeed*Time.deltaTime, 0);
    37.        
    38.         if (DoorMoving == DoorClosed)
    39.             closing = false;
    40.     }
    41. }
     
  2. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Looks like you're checking to see if the door's position exactly equals exactly the position you expect at the open state. However, you translate it at a non-exact value (speed * frame time). Most likely the door never actually equals the DoorOpen position; it just goes past it and continues indefinitely.

    Perhaps instead you could use Lerp instead (http://unity3d.com/support/documentation/ScriptReference/Vector3.Lerp.html) and have the door's position change over a fixed time. Say you want the door to take 1 second to open. Then record the start time of opening, and track the change in time from then. Once 1 second (or greater) is reached, fix the door's position to your final DoorOpen location.
     
  3. Guns are Toys

    Guns are Toys

    Joined:
    May 23, 2010
    Posts:
    21
    Thanks, but I've actually found another way around it. If the door is lower or higher then the set point then it will stop. This works well and I only need 2 points (open, closed) and I can have any number of doors using those 2 points.

    Here's the updated script if anyone wants it or want to improve it:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Door : MonoBehaviour {
    5.    
    6.     public float speed = -1.0F;
    7.     public float Cspeed = 1.0F;
    8.     public Transform door;
    9.     public Transform open;
    10.     public Transform closed;
    11.     bool closing = false;
    12.     bool opening = false;
    13.    
    14.     void OnTriggerEnter (Collider target)
    15.     {
    16.         opening = true;
    17.         closing = false;
    18.     }
    19.    
    20.     void Update ()
    21.     {
    22.         Vector3 DoorOpen = new Vector3(0, open.transform.position.y, 0);
    23.         Vector3 DoorClosed = new Vector3(0, closed.transform.position.y, 0);
    24.         Vector3 DoorMoving = new Vector3(0, door.transform.position.y, 0);
    25.        
    26.         if (opening == true)
    27.             transform.Translate(0, speed*Time.deltaTime, 0);
    28.        
    29.         if (DoorMoving.y < DoorOpen.y)
    30.         {
    31.             opening = false;
    32.             closing = true;
    33.             door.transform.position = new Vector3(door.transform.position.x, open.transform.position.y, door.transform.position.z);
    34.         }
    35.        
    36.         if (closing == true)
    37.             transform.Translate(0, Cspeed*Time.deltaTime, 0);
    38.        
    39.         if (DoorMoving.y > DoorClosed.y)
    40.         {
    41.             closing = false;
    42.             door.transform.position = new Vector3(door.transform.position.x, closed.transform.position.y, door.transform.position.z);
    43.         }
    44.     }
    45. }