Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question how to make an area in which scripts will work

Discussion in 'Scripting' started by evgener2, Sep 6, 2023.

  1. evgener2

    evgener2

    Joined:
    May 29, 2023
    Posts:
    5
    hello, I have a problem, I have a script that opens and closes the door and plus shows a hint. The script has an area in which it acts, on all other objects it works fine, but due to the unusual shape of the door, the area is attached crookedly and I cannot fix it.
    is it possible to do something like "edit collider" like in box collider?

    here is my script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class door2d : MonoBehaviour
    6. {
    7.    
    8.     public GameObject Player;
    9.     public GameObject Hint;
    10.     public GameObject FL;
    11.     public Animator animator;
    12.  
    13.     private bool PlayerDetect;
    14.     public Transform Pos;
    15.     public float width;
    16.     public float height;
    17.     public LayerMask WhatisPlayer;
    18.  
    19.     void Update()
    20.     {
    21.         PlayerDetect = Physics2D.OverlapBox(Pos.position, new Vector2(width, height), 0, WhatisPlayer);
    22.  
    23.         if(PlayerDetect == true)
    24.         {
    25.             Hint.SetActive(true);
    26.             if(Input.GetKeyDown(KeyCode.E))
    27.             {
    28.               StartCoroutine(DoorOpen());
    29.             }
    30.         }
    31.  
    32.         if(PlayerDetect == false)
    33.         {
    34.             Hint.SetActive(false);
    35.         }
    36.     }
    37.  
    38.  
    39.     IEnumerator DoorOpen()
    40.     {
    41.         yield return new WaitForSeconds(0);
    42.         if (FL.GetComponent<Collider2D>().enabled == false)
    43.         {
    44.             FL.GetComponent<Collider2D>().enabled = true;
    45.             animator.SetBool("open on close", false);
    46.  
    47.         }
    48.         else
    49.         {
    50.             FL.GetComponent<Collider2D>().enabled = false;
    51.             animator.SetBool("open on close", true);
    52.         }
    53.     }
    54.  
    55.     private void OnDrawGizmosSelected()
    56.     {
    57.       Gizmos.color = Color.yellow;
    58.       Gizmos.DrawWireCube(Pos.position, new Vector3(width, height, 1));
    59.     }
    60. }
    61.  
    62.  
    63.  
    area is located like this
    hhhh.PNG

    but i would like this
    hhhhh.png
    if anyone has ideas how to fix this or you know how to do what I suggested at the beginning, then write. thank you in advance
     

    Attached Files:

    • hhh.PNG
      hhh.PNG
      File size:
      14 KB
      Views:
      54
    • hh.PNG
      hh.PNG
      File size:
      8.8 KB
      Views:
      40
    Last edited: Sep 6, 2023
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    evgener2 likes this.
  3. evgener2

    evgener2

    Joined:
    May 29, 2023
    Posts:
    5
    thanks for the tip, but i couldn't figure out how to put this in my script, so i decided to use trigger
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class door2d : MonoBehaviour
    6. {
    7.    
    8.     public GameObject Hint;
    9.     public GameObject FL;
    10.     public Animator animator;
    11.  
    12.     void Update()
    13.     {
    14.         if(Hint.GetComponent<SpriteRenderer>().enabled == true)
    15.         {
    16.             if(Input.GetKeyDown(KeyCode.E))
    17.             {
    18.                 StartCoroutine(DoorOpen());
    19.             }
    20.         }
    21.        
    22.     }
    23.  
    24.     IEnumerator DoorOpen()
    25.     {
    26.         yield return new WaitForSeconds(0);
    27.         if (FL.GetComponent<Collider2D>().enabled == false)
    28.         {
    29.             FL.GetComponent<Collider2D>().enabled = true;
    30.             animator.SetBool("open on close", false);
    31.  
    32.         }
    33.         else
    34.         {
    35.             FL.GetComponent<Collider2D>().enabled = false;
    36.             animator.SetBool("open on close", true);
    37.         }
    38.     }
    39.  
    40.     void OnTriggerStay2D(Collider2D other)
    41.     {
    42.         Hint.SetActive(true);
    43.     }
    44.  
    45.     void OnTriggerExit2D(Collider2D other)
    46.     {
    47.         Hint.SetActive(false);
    48.     }
    49. }
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    I thought you were asking about editing a region in the editor?!?

    I really don’t understand what the problem is here. No idea what you mean by crooked or why you need something like Edit Collider.
     
    wideeyenow_unity likes this.
  5. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    If you're asking about the trigger zone of said door, yes you can set that within the box collider method(button).

    How exactly is this giving you issues? Because the box collider will be parented to the object itself, normally using local positions. Which also you can see physically what the box collider reaches to(each extent), so I'm not grasping where your issue lies? Can it possibly be something wrong with the code, that executes said door?