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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Can't activate bool properly

Discussion in 'Scripting' started by OverBoosted, Jun 2, 2022.

  1. OverBoosted

    OverBoosted

    Joined:
    Sep 10, 2021
    Posts:
    4
    The problem I have is if I have the parts of the code that turns the bools false the bools can't be true, how can I fix this?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Screwable : MonoBehaviour
    6. {
    7.     //Used for size of bolt to need a wrench of same size
    8.     public float Size;
    9.  
    10.     //Value of how much the bolt should turn each each time it gets input
    11.     public float rotValue;
    12.    
    13.     //Once the transform rotation z reaches this I want "Bolted" to turn true
    14.     public float ORZ;
    15.  
    16.     //Once the transform rotation z reaches this I want "Stop" to turn true
    17.     public float SORZ;
    18.  
    19.     //Once true bolt can't be turned anymore
    20.     //Used so bolt can't screw in too much
    21.     public bool Bolted;
    22.    
    23.     //Once true bolt can't be turned anymore
    24.     //Used so bolt can't unscrew too much
    25.     public bool Stop;
    26.  
    27.     //Used if bolt is upside down
    28.     public bool Direction;
    29.  
    30.     void Start()
    31.     {
    32.         Bolted = false;
    33.         Stop = true;
    34.     }
    35.  
    36.     void Update()
    37.     {
    38.         if (this.transform.rotation == Quaternion.Euler(this.transform.rotation.x, this.transform.rotation.y, ORZ))
    39.         {
    40.             Bolted = true;
    41.         }
    42.         if(this.transform.rotation != Quaternion.Euler(this.transform.rotation.x, this.transform.rotation.y, ORZ))
    43.         {
    44.             Bolted = false;
    45.         }
    46.  
    47.         if(this.transform.rotation == Quaternion.Euler(this.transform.rotation.x, this.transform.rotation.y, SORZ))
    48.         {
    49.             Stop = true;
    50.         }
    51.         if(this.transform.rotation != Quaternion.Euler(this.transform.rotation.x, this.transform.rotation.y, SORZ))
    52.         {
    53.             Stop = false;
    54.         }
    55.     }
    56. }
    57.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    Rotations are made of floating point numbers.

    You cannot reliably check floating point numbers for equality.

    Here's why:

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

    The above expression is not degrees... you are attempting to use it as if it was. Don't do that.

    Here's why:

    https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html

    (*Wow, first time I've quoted @StarManta twice in one post! )
     
  3. OverBoosted

    OverBoosted

    Joined:
    Sep 10, 2021
    Posts:
    4
    So, if I create floats for each rotation value and set them to the correct values it should work?