Other
if (transform.position.x == -38.12f) not working :\
I've created an if statement and it's not working. Code: public class poruszanie : MonoBehaviour {
public static int Lewo;
public static int Prawo;
public static int Poz;
public Rigidbody2D gracz;
public Transform graczPos;
void Start () {
Poz = 1;
}
void Update ()
{
if (Prawo == 1 && Poz >= 1 && Poz < 4)
{
gracz.velocity = new Vector2 (100, 0);
Debug.Log ("Yea");
Prawo = 0;
}
if (graczPos.position.x == -38.12f)
{
gracz.velocity = new Vector2 (0, 0);
Poz++;
Debug.Log("aeY");
}
}
}
Answer by Landern · Dec 05, 2016 at 02:12 PM
try using Mathf.Approximately, floats can be inacurrate when calculated and may not just be "-38.12f" and might be "-38.12000001f" instead which would not be true in your case.
change line 18 and to the following and see if it works better for you.
if (Mathf.Approximately(graczPos.position.x, -38.12f))
Answer by UnityCoach · Dec 05, 2016 at 02:13 PM
This will only work if position x is exactly -38.12, not sure this is what you want to to.
Maybe try
if (graczPos.position.x <= -38.12f)
Answer by MrPlayerowy · Dec 06, 2016 at 06:32 AM
Sometimes, when objects rotate they change their position, try jikkou code or make a collider with trigger that in update will check the graczPos is in the trigger.
Follow this Question
Related Questions
Random.Range returns either 1 or 2 2 Answers
SImple, but how do I check a game object's position in an If statement? 1 Answer
SqrMagnitude intermittently doesn't work for if statement on prefab. (C sharp) 1 Answer
Two IF Statements not working in one method 2 Answers
[C#] If (==) statement not working. Values checked. 1 Answer