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
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...
what is the error rate comparing Vector3’s to be == ? I seem to have about 20% of my objects missing when I compare to see if the objects are there, with : if (Vector3(X, Y, Z) == Vector3 (X, Y, Z)){ do something;} I would prefer to compare them...
here is a similar awnser that explains the issue further.
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;
}
}
`