Question by
Imenbm · Jan 26, 2017 at 11:24 AM ·
gameobjectpositioncompare
comparing two gameobject
Hello i need your help guys, i want to compare a position of two gameobjects and if there equal, the score +1 this is my code .the problem that i always have a "0" for the score
void Update () {
transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * Time.deltaTime * 40);
transform.Translate(Vector3.forward * Input.GetAxis("Vertical") * Time.deltaTime * 40);
tmpPo = transform.position;
tmpPo2 = cube1.transform.position;
if ((Mathf.Approximately(tmpPo.x,tmpPo2.x))) //& (Mathf.Approximately(tmpPo.z, tmpPo2.z)))
{
sc++;
}
Text singleText = mytext1.GetComponentInChildren<Text>();
singleText.text = "tmp: " + tmpPo + " tmp2: "+ tmpPo2;
Text singleText2 = mytext2.GetComponentInChildren<Text>();
singleText2.text = "score: " + sc;
}
Comment
Best Answer
Answer by NoseKills · Jan 26, 2017 at 05:23 PM
When you use Matf.Approximately, the values have to be almost identical for it to return true. It might be impossible to get them close enough depending on your control method.
You should try to see how far apart they are
singleText.text = Mathf.Abs( tmpPo.x - tmpPo2.x).ToString();
When you find a value you'd like to be considered "equal position", change your condition to use that value
if (Mathf.Abs( tmpPo.x - tmpPo2.x) < 0.01f)
or such