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

help with the code

Discussion in 'Scripting' started by Slabada, May 21, 2022.

  1. Slabada

    Slabada

    Joined:
    May 2, 2021
    Posts:
    50
    hello, maybe the question is stupid or funny, but I don't know how to solve it.
    There is a code, there is a condition in the code, if the character position is equal to the position of some object, then the menu is activated through SetActive, in the same code there is a method that closes the menu through SetActive through a button. But here such a thing turned out, it is impossible to close the menu because of the condition that is indicated.
    Code (CSharp):
    1.     private void OpenMenu()
    2.     {
    3.        
    4.         if (transform.position.x == GameManager.Personage.transform.position.x)
    5.             Menu.SetActive(true);
    6.     }
    7.  
    8.     public void Exit()
    9.     {
    10.         Menu.SetActive(false);
    11.     }
    Called in Update.
    Please tell me how to solve this problem?
     
  2. iMobCoding

    iMobCoding

    Joined:
    Feb 13, 2017
    Posts:
    160
    Make something like:
    Code (CSharp):
    1. private bool _isMenuOpen;
    and then statement like:
    Code (CSharp):
    1.  
    2. if (!_isMenuOpen && transform.position.x ==  GameManager.Personage.transform.position.x)
    3. {
    4.     _isMenuOpen = true;
    5.     Menu.SetActive(true);
    6. }
    7.  
    and don't forget to mark it false later:
    Code (CSharp):
    1. public void Exit()
    2. {
    3.     Menu.SetActive(false);
    4.     _isMenuOpen = false;
    5. }
    Though, this will not help you much if your object stays in the same position, because the condition will be true again. Maybe try to move it manually?
     
    Slabada and Hikiko66 like this.
  3. Slabada

    Slabada

    Joined:
    May 2, 2021
    Posts:
    50
    Thanks for the answer, but unfortunately it doesn't work. Because the object does not move anywhere from the point at which it changes true to false and the menu cannot be closed either, it just returns false immediately.
     
  4. iMobCoding

    iMobCoding

    Joined:
    Feb 13, 2017
    Posts:
    160
    Yes I thought so. Then store the position of your object in the outer variable at the time the menu opens and then compare if object moved and if not - don't allow the menu to open.

    Something like this:
    Code (CSharp):
    1. private float _previousXPos = float.MaxValue;
    2.  
    3. private void Update ()
    4. {
    5.     float currentXPos = transform.position.x;
    6.  
    7.     if (currentXPos != _previousXPos && currentXPos == GameManager.Personage.transform.position.x)
    8.     {
    9.         _previousXPos = currentXPos;
    10.         OpenMenu();
    11.     }
    12. }
    13.  
    and leave everything else like it was in your original message. Also, not sure which object moves here for you - transform.position.x or GameManager.Personage.transform.position.x? If it's the later then use that instead in line 5.

    Though, have in mind that comparing float values like this doesn't always work as expected. You should always add some margin to comparison like using the Mathf.Epsilon value
     
    Last edited: May 21, 2022
    Slabada likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    You are always going to have issues with this. Do not compare floating point values for equality, ESPECIALLY floating point values coming back from an internal game engine property like this.

    Here's why:

    https://starmanta.gitbooks.io/unitytipsredux/content/floating-point.html

    Also, while you're at it, if you have more than one or two dots (.) in a single statement, you're just being mean to yourself.

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    Break it up, practice social distancing in your code, one thing per line please.

    "Programming is hard enough without making it harder for ourselves." - angrypenguin on Unity3D forums
     
    Slabada likes this.
  6. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    530
    A typical solution that would solve the initial problem and also any potential issues with floating point equality is having some hysteresis.


    Code (CSharp):
    1.  
    2. if (distance < r1 && canOpen) {
    3.    open();
    4.    canOpen = false;
    5. }
    6. // require moving further away  than opening distance
    7. // before allowing to open again
    8. if (distance > r1 + k) {
    9.    canOpen = true;
    10. }
     
    Slabada likes this.