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 detect transform

Discussion in '2D' started by Deleted User, Jul 22, 2020.

  1. Deleted User

    Deleted User

    Guest

    Hi. Im trying to make a game but my code isn't running when the transform is 14. This is my code
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Taco : MonoBehaviour
    4. {
    5.  
    6.     public Rigidbody2D rigidbody;
    7.     public float MoveSpeed = 100f;
    8.     public int Movecount = 0;
    9.  
    10.     void Update()
    11.     {
    12.         if(transform.position.x == 14f)
    13.         {
    14.             Debug.Log("Y = 14");
    15.         }
    16.     }
    17.  
    18.     void FixedUpdate()
    19.     {
    20.         if(Movecount <= 5)
    21.         {
    22.             rigidbody.AddForce(transform.right * MoveSpeed);
    23.             Movecount = Movecount + 1;
    24.         }
    25.     }
    26.  
    27. }
    28.  
     
    Last edited by a moderator: Jul 22, 2020
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    You should not compare floating point values like this as small errors or inability to exactly represent certain numbers using floating point will cause problems. For instance, it might be 14.00002.

    This is why functions like the following are important: Mathf.Approximately
     
  3. Deleted User

    Deleted User

    Guest

    Its still not working but I might not be doing to right this is my new script
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Taco : MonoBehaviour
    4. {
    5.  
    6.     public Rigidbody2D rigidbody;
    7.     public float MoveSpeed = 100f;
    8.     public int Movecount = 0;
    9.  
    10.     void Update()
    11.     {
    12.         if(Mathf.Approximately(transform.position.x, 15f / 15f))
    13.         {
    14.             Debug.Log("Y = 14");
    15.         }
    16.     }
    17.  
    18.     void FixedUpdate()
    19.     {
    20.         if(Movecount <= 5)
    21.         {
    22.             rigidbody.AddForce(transform.right * MoveSpeed);
    23.             Movecount = Movecount + 1;
    24.         }
    25.     }
    26.  
    27. }
     
  4. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    491
    I don't think that is how you use MathfApproximately, as 15f/15f would be 1f and not the 14 that you are expecting for your x position.
    You should be able to do it a couple of different ways:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if(Mathf.Approximately(1f, transform.position.x / 14f))
    4.         {
    5.             Debug.Log("Y = 14");
    6.         }
    7.     }
    Or even simpler:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if(Mathf.Approximately(transform.position.x, 14f))
    4.         {
    5.             Debug.Log("Y = 14");
    6.         }
    7.     }
     
    Last edited: Jul 23, 2020
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    Yes, you've taken the example literally but seem to have not read the associated description. You simply put in the two values you want to compare. The example which puts in 10.0f / 10.0f demonstrates how float operations might not give you exactly what you want. It might not be 1.0f but 1.0001f etc.

    Code (CSharp):
    1. // Is the X position 14?
    2. if(Mathf.Approximately(transform.position.x, 14f))
    The thing is, your code is pretty sensitive. Why would it be exactly 14? It's more likely you want to check:
    Code (CSharp):
    1. if (transform.position.x >= 14f)
    or check a range. Working with exact values is going to make it sensitive which is why when working with floats you work with small ranges or thresholds.
     
    Last edited: Jul 23, 2020
  6. Peanut8

    Peanut8

    Joined:
    May 17, 2018
    Posts:
    10
    Code (CSharp):
    1. if (transform.position.x >= 14f && transform.position.x <= 15f )
    That would do the thing :D
     
  7. Deleted User

    Deleted User

    Guest

    I fixed the script. Thanks! :)
     
    Peanut8 likes this.