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

Resolved Mistake - Moved Thread

Discussion in 'Editor & General Support' started by BenevolenceGames, Mar 15, 2021.

  1. BenevolenceGames

    BenevolenceGames

    Joined:
    Feb 17, 2021
    Posts:
    128
    I reposted in appropriate Forum. I'm not sure how to delete this thread though...
     
    Last edited: Mar 15, 2021
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Don't test floats with equals "==". See other threads on floating point accuracy issues. Your line 4 has me concerned you may be creating a new material every frame. You should create the material once and cache it. See this link:
    https://docs.unity3d.com/ScriptReference/Renderer-material.html

    But on your question, you're adding or subtracting 0.1f every frame, regardless of frame rate. I suggest you instead add a multiple of Time.deltaTime. This will make it move at the same speed regardless of frame rate, and you just adjust the multiplier to get the speed you're looking for. Set the multiple to a smaller number to slow down the transition, a larger number to speed it up.

    Code (csharp):
    1. public float SpeedMultiplier = 0.2f;  //adjust this in the inspector until if feels right
    Then take line 28 for example, try changing to below, and everywhere else you're doing similar.
    Code (csharp):
    1.     red = red - (SpeedMultiplier * Time.deltaTime);
     
  3. BenevolenceGames

    BenevolenceGames

    Joined:
    Feb 17, 2021
    Posts:
    128
    Ok, I'm looking into the material cache link now. I don't know how else to test the if statement there because a single = throws an error. I will investigate existing material to find a better method to test the variable there. I moved the thread, btw, to the same title under the Scripting forum. Thank you for a speedy response.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Use >= or <=. The problem is after you add 0.1f ten times, you think the float is exactly 1f, but it probably is something like 1.0000004f or similarly close but not exactly 1f. So your equals == comparison returns false.
     
  5. BenevolenceGames

    BenevolenceGames

    Joined:
    Feb 17, 2021
    Posts:
    128
    Ahhh! That makes sense. I will try that also, but I think for what I want to do it would be better to go a different route. a Math function or something perhaps...? I'm not really sure. It seems the general consensus is floats are too inaccurate to do something this precise? If I'm understanding that right?
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Floats are just too inaccurate to use the equals == comparison. You can certainly use them for this kind of thing though.
     
  7. BenevolenceGames

    BenevolenceGames

    Joined:
    Feb 17, 2021
    Posts:
    128
    Ok, so I changed the script up a little, but it still isn't changing colors. It spawns a red cube and just stays red.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Cube : MonoBehaviour
    6. {
    7.     public MeshRenderer Renderer;
    8.     private Material material;
    9.  
    10.  
    11.     void Start()
    12.     {    
    13.         //Randomizes starting position and size within set parameters
    14.         float startX = Random.Range(3.0f, 5.5f);
    15.         float startY = Random.Range(4.0f, 6.5f);
    16.         float startZ = Random.Range(0.5f, 2.0f);
    17.         float startSize = Random.Range(0.7f, 1.9f);
    18.         transform.position = new Vector3(startX, startY, startZ);
    19.         transform.localScale = Vector3.one * startSize;
    20.     }
    21.    
    22.     void Update()
    23.     {
    24.         //Roatation of cube
    25.         transform.Rotate(10.0f * Time.deltaTime, 0.0f, 0.0f);
    26.         //Not really sure what this does
    27.         material = Renderer.material;
    28.  
    29.         //Color assignments
    30.         float red = 1.0f;
    31.         float green = 0.0f;
    32.         float blue = 0.0f;
    33.         Color matColor = new Color(red, green, blue, 1.0f);
    34.  
    35.         //The idea here is to change the value of the floats every frame and check for 1.0 values then change the value being affected accordingly.
    36.         if (red >= 1.0f)
    37.         {
    38.             if (blue > 0.0f)
    39.             {
    40.                 blue = blue - 0.1f * Time.deltaTime;
    41.             }
    42.             if (blue == 0.0f)
    43.             {
    44.                 green = green + 0.1f;
    45.             }
    46.         }
    47.  
    48.         if (green >= 1.0f)
    49.         {
    50.             if (red > 0.0f)
    51.             {
    52.                 red = red - 0.1f;
    53.             }
    54.             if (red == 0.0f)
    55.             {
    56.                 blue = blue + 0.1f;
    57.             }
    58.         }
    59.  
    60.         if (blue >= 1.0f)
    61.         {
    62.             if (green > 0.0f)
    63.             {
    64.                 green = green - 0.1f;
    65.             }
    66.             if (green == 0.0f)
    67.             {
    68.                 red = red + 0.1f;
    69.             }
    70.         }
    71.  
    72.         //Assigns material a new color every frame
    73.         material.color = matColor;
    74.     }
    75. }
    76.  
    -- I think I'm just going to have to do some more learning here. I may have bit off a bit much for this point, or maybe I'm just missing something incredibly simple.... I'm not sure, but I feel dumb right now bud.
     
  8. BenevolenceGames

    BenevolenceGames

    Joined:
    Feb 17, 2021
    Posts:
    128
    I figured it out, I needed to define the variables outside of the Update class so it wasn't constantly being reassigned to a value of R=1 G=0 B=0
     
  9. BenevolenceGames

    BenevolenceGames

    Joined:
    Feb 17, 2021
    Posts:
    128
    Hey, question. I know this thread is ancient, but regarding the float inaccuracy issue. I have another implementation that relies on an accurate 0.5, is there a way to only compare against a certain number of points after the decimal? Like assign the variable to a string and then feed it into the final equation from the string or something?