Search Unity

Open/close door when the player is close to it.

Discussion in 'Scripting' started by matiasges, Jul 14, 2021.

  1. matiasges

    matiasges

    Joined:
    Jan 24, 2021
    Posts:
    142
    I set up a script that allows me to open/close doors by dragging the mouse up and down and it works right. However what I want to achieve is that the player can only open/close doors when he is near the door.

    The code is the following:
    Code (CSharp):
    1.  
    2. public class door : MonoBehaviour
    3. {
    4.     public float ySensitivity = 300f;
    5.     public float frontOpenPosLimit = 45;
    6.     public float backOpenPosLimit = 45;
    7.     public GameObject frontDoorCollider;
    8.     public GameObject backDoorCollider;
    9.     bool moveDoor = false;
    10.     DoorCollision doorCollision = DoorCollision.NONE;
    11.     // Use this for initialization
    12.     void Start()
    13.     {
    14.         StartCoroutine(doorMover());
    15.     }
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         if (Input.GetMouseButtonDown(0))
    20.         {
    21.             Debug.Log("Mouse down");
    22.             RaycastHit hitInfo = new RaycastHit();
    23.             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo))
    24.             {
    25.                 if (hitInfo.collider.gameObject == frontDoorCollider)
    26.                 {
    27.                     moveDoor = true;
    28.                     Debug.Log("Front door hit");
    29.                     doorCollision = DoorCollision.FRONT;
    30.                 }
    31.                 else if (hitInfo.collider.gameObject == backDoorCollider)
    32.                 {
    33.                     moveDoor = true;
    34.                     Debug.Log("Back door hit");
    35.                     doorCollision = DoorCollision.BACK;
    36.                 }
    37.                 else
    38.                 {
    39.                     doorCollision = DoorCollision.NONE;
    40.                 }
    41.             }
    42.         }
    43.         if (Input.GetMouseButtonUp(0))
    44.         {
    45.             moveDoor = false;
    46.             Debug.Log("Mouse up");
    47.         }
    48.     }
    49.     IEnumerator doorMover()
    50.     {
    51.         bool stoppedBefore = false;
    52.         float yRot = 0;
    53.         while (true)
    54.         {
    55.             if (moveDoor)
    56.             {
    57.                 stoppedBefore = false;
    58.                 Debug.Log("Moving Door");
    59.                 yRot += Input.GetAxis("Mouse Y") * ySensitivity * Time.deltaTime;
    60.                 //Check if this is front door or back
    61.                 if (doorCollision == DoorCollision.FRONT)
    62.                 {
    63.                     Debug.Log("Pull Down(PULL TOWARDS)");
    64.                     yRot = Mathf.Clamp(yRot, -frontOpenPosLimit, 0);
    65.                     Debug.Log(yRot);
    66.                     transform.localEulerAngles = new Vector3(0, -yRot, 0);
    67.                 }
    68.                 else if (doorCollision == DoorCollision.BACK)
    69.                 {
    70.                     Debug.Log("Pull Up(PUSH AWAY)");
    71.                     yRot = Mathf.Clamp(yRot, 0, backOpenPosLimit);
    72.                     Debug.Log(yRot);
    73.                     transform.localEulerAngles = new Vector3(0, yRot, 0);
    74.                 }
    75.             }
    76.             else
    77.             {
    78.                 if (!stoppedBefore)
    79.                 {
    80.                     stoppedBefore = true;
    81.                     Debug.Log("Stopped Moving Door");
    82.                 }
    83.             }
    84.             yield return null;
    85.         }
    86.     }
    87.     enum DoorCollision
    88.     {
    89.         NONE, FRONT, BACK
    90.     }
    91. }
    92.  
    Right now I can open/close doors when the player is at any distance. How can I achieve for the player to open/close doors when he is near to it? some sort of triggers?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Definitely a very common approach.

    Screen Shot 2021-07-13 at 4.20.24 PM.png

    NOTE: some of those tutorials may be auto-open. Your job is to isolate the condition that determines "I am close" via trigger, and connect it to your current on-demand open.
     
    matiasges likes this.
  3. matiasges

    matiasges

    Joined:
    Jan 24, 2021
    Posts:
    142
    Update: So I set up this code for the trigger collider:

    Code (CSharp):
    1. public class triggerDoor : MonoBehaviour
    2. {
    3.  
    4.  
    5.     void OnTriggerEnter(Collider other)
    6.     {
    7.         // enable script on GameObject when player enters trigger
    8.         GetComponentInChildren<door>().enabled = true;
    9.     }
    10.     void OnTriggerExit(Collider other)
    11.         {
    12.         // disable script on GameObject when player enters trigger
    13.         GetComponentInChildren<door>().enabled = false;
    14.         }
    15. }
    16.  
    It works at some extent. Because when I'm in a certain position I can close/open the door without clicking. It gets sort of stuck and when I move the mouse up and down the door opens and closes, no matter if I click or not.
     
  4. matiasges

    matiasges

    Joined:
    Jan 24, 2021
    Posts:
    142
    Nevermind. I changed it for .moveDoor = false;