I have asked this question three times now (including this one), and either no one has been able to help me or no one has understood the question. I am going to try one more time to explain my issue simply because, after nearly two weeks of trying to fix this, I have had no luck at all.
I am trying to make a snap to cover system that is flexible enough to allow players to create tile based levels. Therefor, I have a bunch of prefabs that look similar to the picture below. I am using empty game objects (colored circles in the figure) to find cover pieces. The code I am using to find the end of walls and other cover objects will be below. My issue is, in the inspector, the yellow game object in the figure is shown to be in the same position as the green object from the next wall tile over to the left and the green would be in the same position as the yellow from the tile on the right. When I am running my code though, the positions are shown to be different which breaks the whole point of them beginning in the same spot. No code is working on the objects other than the code below and there is no movement in the code. When printing out the distances between the yellows and greens that should be in the same position, there are odd consistencies. There is no pattern in the distances, but the distances are always the same. Some distances come out to be .75, some to 5, and others to 2.70983, but those points will always be that distance apart each time I run the code.
I am very confused as to what is happening and have no idea what could be going wrong. If anyone has any thoughts, answers, or suggestions, they would be much appreciated. Thanks so much!
For reference, the yellow object has the tag “CoverL”, the green one has “CoverR”, and the other 3 have “Cover”.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PointType : MonoBehaviour {
public GameObject[] coverpoints;
public GameObject leftJumpPoint;
public GameObject rightJumpPoint;
private Vector3 leftDist;
private Vector3 rightDist;
void Start () {
List<GameObject> lefts = new List<GameObject>();
List<GameObject> rights = new List<GameObject>();
GameObject[] obj1 = GameObject.FindGameObjectsWithTag("CoverL");
GameObject[] obj2 = GameObject.FindGameObjectsWithTag("CoverR");
List<GameObject> obj = new List<GameObject>();
obj.AddRange(obj1);
obj.AddRange(obj2);
foreach(GameObject go in obj){
if(go != this.gameObject && (Mathf.Round(go.transform.parent.parent.eulerAngles.y) == Mathf.Round(transform.parent.parent.eulerAngles.y) && Mathf.Round(transform.parent.parent.eulerAngles.y) == 0f))
{
if(go.transform.position.x == transform.position.x)
{
if(go.transform.position.z < transform.position.z && go.tag == "CoverL")
{
if(FindClosest(GameObject.FindGameObjectsWithTag("CoverR"), go).transform.position != transform.position)
{
Debug.Log("0left");
Debug.Log (FindClosest(GameObject.FindGameObjectsWithTag("CoverR"), go).transform.position - transform.position);
lefts.Add(go);
}
}
else if(go.transform.position.z > transform.position.z && go.tag == "CoverR")
{
if(FindClosest(GameObject.FindGameObjectsWithTag("CoverL"), go).transform.position != transform.position)
{
Debug.Log("0right");
Debug.Log (FindClosest(GameObject.FindGameObjectsWithTag("CoverR"), go).transform.position - transform.position);
rights.Add(go);
}
}
}
}
else if(go != this.gameObject && (Mathf.Round(go.transform.eulerAngles.y) == Mathf.Round(transform.eulerAngles.y) && Mathf.Round(transform.eulerAngles.y) == 90f))
{
if(go.transform.position.z == transform.position.z)
{
if(go.transform.position.x < transform.position.x && go.tag == "CoverL")
{
if(FindClosest(GameObject.FindGameObjectsWithTag("CoverR"), go).transform.position != transform.position)
{
Debug.Log("90");
Debug.Log (FindClosest(GameObject.FindGameObjectsWithTag("CoverR"), go).transform.position - transform.position);
lefts.Add(go);
}
}
else if(go.transform.position.x > transform.position.x && go.tag == "CoverR")
{
if(FindClosest(GameObject.FindGameObjectsWithTag("CoverL"), go).transform.position != transform.position)
{
rights.Add(go);
}
}
}
}
else if(go != this.gameObject && (Mathf.Round(go.transform.eulerAngles.y) == Mathf.Round(transform.eulerAngles.y) && Mathf.Round(transform.eulerAngles.y) == 180f))
{
if(go.transform.position.x == transform.position.x)
{
if(go.transform.position.z > transform.position.z && go.tag == "CoverL")
{
if(FindClosest(GameObject.FindGameObjectsWithTag("CoverR"), go).transform.position != transform.position)
{
lefts.Add(go);
}
}
else if(go.transform.position.z < transform.position.z && go.tag == "CoverR")
{
if(FindClosest(GameObject.FindGameObjectsWithTag("CoverL"), go).transform.position != transform.position)
{
rights.Add(go);
}
}
}
}
else if(go != this.gameObject && (Mathf.Round(go.transform.eulerAngles.y) == Mathf.Round(transform.eulerAngles.y) && Mathf.Round(transform.eulerAngles.y) == 270f))
{
if(go.transform.position.z == transform.position.z)
{
if(go.transform.position.x > transform.position.x && go.tag == "CoverL")
{
if(FindClosest(GameObject.FindGameObjectsWithTag("CoverR"), go).transform.position != transform.position)
{
lefts.Add(go);
}
}
else if(go.transform.position.x < transform.position.x && go.tag == "CoverR")
{
if(FindClosest(GameObject.FindGameObjectsWithTag("CoverL"), go).transform.position != transform.position)
{
rights.Add(go);
}
}
}
}
//Debug.Log (go.transform.position);
//foreach(GameObject go1 in lefts)
//Debug.Log (go1);
}
coverpoints = new GameObject[] {FindClosest(lefts), FindClosest(rights)};
//second closest left and right are leftJumpPoint and rightJumpPoint
}
GameObject FindClosest(GameObject[] obj)
{
GameObject closest = null;
float coverDist = Mathf.Infinity;
foreach(GameObject go in obj)
{
if(go != this.gameObject)
{
Vector3 diff = go.transform.position - transform.position;
float curDistance = diff.sqrMagnitude;
if (curDistance < coverDist)
{
closest = go.gameObject;
coverDist = curDistance;
}
}
}
return closest;
}
GameObject FindClosest(GameObject[] obj, GameObject tar)
{
GameObject closest = null;
float coverDist = Mathf.Infinity;
foreach(GameObject go in obj)
{
if(go != this.gameObject)
{
Vector3 diff = go.transform.position - tar.transform.position;
float curDistance = diff.sqrMagnitude;
if (curDistance < coverDist)
{
closest = go.gameObject;
coverDist = curDistance;
}
}
}
return closest;
}
GameObject FindClosest(List<GameObject> obj)
{
GameObject closest = null;
float coverDist = Mathf.Infinity;
foreach(GameObject go in obj)
{
if(go != this.gameObject)
{
Vector3 diff = go.transform.position - transform.position;
float curDistance = diff.sqrMagnitude;
if (curDistance < coverDist)
{
closest = go.gameObject;
coverDist = curDistance;
}
}
}
return closest;
}
GameObject[] GetCoverPoints()
{
return coverpoints;
}
}