if statement not runing when it should (Fixed)
in my game's code this code will not work
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class pozkill : MonoBehaviour {
[SerializeField]
bool debug = false;
[SerializeField]
Vector3 poz_to_kill = new Vector3(0f,0f,0f);
[SerializeField]
bool show_poz = false;
[SerializeField]
float time_intill_delt = 0;
float time = 0;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (show_poz == true)
{
Debug.Log("poz is: " + this.transform.position);
show_poz = false;
}
if (this.transform.position == poz_to_kill)
{
if (debug == true)
{
Debug.Log("pos is good");
}
if (time >= time_intill_delt)
{
Destroy(this);
}
if (time <= time_intill_delt)
{
time = time + Time.deltaTime;
if (debug == true)
{
Debug.Log("time = " + time + "time_intill_delt = " + time_intill_delt);
}
}
}
else
{
if (debug == true)
{
Debug.Log(this.transform.position + "!=" + poz_to_kill);
}
}
}
}
the 2nd if statement will not run when this.transform.position and poz_to_kill are equal
Answer by unity_user_223 · Sep 27, 2018 at 04:51 AM
FIx using System.Collections; using System.Collections.Generic; using UnityEngine; public class pozkill : MonoBehaviour { GameObject perent; [SerializeField] bool debug = false; [SerializeField] Vector3 poz_to_kill = new Vector3(0f,0f,0f); [SerializeField] bool show_poz = false; [SerializeField] float time_intill_delt = 0; [SerializeField] float limite = 0.2f; [SerializeField] string name = ""; float negetivlimite_help =0; float negetivlimite = 0; float time = 0; float X =0; float Y =0; float Z =0; string XYZ = ""; // Use this for initialization void Start () { negetivlimite_help = limite * 2; negetivlimite = limite - negetivlimite_help; perent = GameObject.Find(name); } // this.transform.position, poz_to_kill // Update is called once per frame void Update() { XYZ = ""; if (show_poz == true) { Debug.Log("poz is: " + this.transform.position); show_poz = false; } X = this.transform.position.x - poz_to_kill.x; if (X < limite) { if (X > negetivlimite) { X = 0; } } XYZ = XYZ + X; Y = this.transform.position.y - poz_to_kill.y; if (Y < limite) { if (Y > negetivlimite) { Y = 0; } } XYZ = XYZ + Y; Z = this.transform.position.z - poz_to_kill.z; if (Z < limite) { if (Z > negetivlimite) { Z = 0; } } XYZ = XYZ + Z; if (debug == true) { Debug.Log(XYZ); } if (XYZ == "000") { if (debug == true) { Debug.Log("pos is good"); } if (time >= time_intill_delt) { Destroy(perent); } if (time <= time_intill_delt) { time = time + Time.deltaTime; if (debug == true) { Debug.Log("time = " + time + "time_intill_delt = " + time_intill_delt); } } } else { if (debug == true) { Debug.Log(this.transform.position + "!=" + poz_to_kill); } } debug = false; } }
Answer by hans_ruebe · Sep 25, 2018 at 12:13 PM
Ok you should try to write a funktion that calcutlates the distance between the this.transform.position and the poz_to_kill because the (==) method seems to be flawed in this case...
Therefore you should write a function like this...
public bool V3Equal(Vector3 a, Vector3 b)
{
return Vector3.SqrMagnitude(a - b) < 0.0001;
}
and in your origianl code, use this :
if (V3Equal(this.transform.position, poz_to_kill))
{
if...
https://answers.unity.com/questions/395513/vector3-comparison-efficiency-and-float-precision.html
here is a similar awnser that explains the issue further.
Unity already has overloaded the ==
operator, so no need to create your own function.
public const float kEpsilon = 0.00001F
public static bool operator==(Vector3 lhs, Vector3 rhs)
{
return Sqr$$anonymous$$agnitude(lhs - rhs) < kEpsilon * kEpsilon;
}
Your answer
Follow this Question
Related Questions
Unity freeze when open script,I keep trying to open a script, but it freezes Unity. Please help. 1 Answer
Moving relative to camera 0 Answers
unable to use dontdestroy on load 1 Answer
Trying to get audiosource to play once 1 Answer
How do I not trigger animation during play state of the triggered animation. 1 Answer