How can I compare two Transforms(GameObjects)?

by Benzi Avrumi   Last Updated November 09, 2018 00:13 AM

I'm trying now:

foreach (GameObject obj in allobj)
                {
    CompareTransformsData(obj) == false)
                }

The method: CompareTransformsData

public static bool CompareTransformsData(GameObject objToCompare)
        {
            List<GameObject> results = allobj;
            bool identical = true;

            foreach(GameObject obj in results)
            {
                if (GameObject.ReferenceEquals(obj, objToCompare))
                {
                    return identical;
                }
            }

            return identical;
        }

What I want to do is to check if obj is identical to any of the gameobjects in allobj.

The first problem is the loop I guess I'm checking the same objects and messed it all.

Second problem the comparison is wrong since comparing floats never accurate.

I was noticed and saw that maybe I should do this: But I'm not sure how to do it in my code.

"You are not getting desired result because of the comparers. You should not compare floating point values/Vectors/Quaternions using "==". As they are based on floats & many a times they wont be accurate enough. Even the cloned objects will have float values 1.000001 or 0.999999.

The better option would be for vectors use "Vector3.distance" or "Vector3.Equals" and for Quternions you can use "Quaternion.Angle" or Quaternion.Equals to check the corresponding result.

ie obj.transform.position.Equals(objToCompare.transform.position) & obj.transform.quaternion.Equals(objToCompare.transform.quaternion). This should return the proper results but there is a chance that the results might not come up same if that happens you can use

ie Vector3.Distance(obj.transform.position, objToCompare.transform.position) < "Margin of difference". Quaternion.Angle(transform.rotation, rotation)<"Margin of difference"

You can specify your own value like for margin of difference you would allow."

Tags : unity c#


Related Questions


How to animate objects in Unity 3D

Updated July 12, 2015 13:05 PM