Game Object Positioning
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;
}
}
Answer by Owen-Reynolds · Dec 12, 2015 at 12:31 AM
It sounds like you're just having trouble using the Editor. Some things that might help:
o If an object has a parent, the Inspector shows it's local coordinates (relative to the parent.) If a bunch of items all have the same parent, this is fine (maybe the Inspector says they have x=0 and x=2, and for real they have x=50 and x=52. They're still two units away.) But watch out for parents with non-(1,1,1) scales.
If you want, you can take an object out of the parent, look at the real coords, then put it back.
o You can Pause the game, go into the Scene window, and move around and Select objects. If you think things are moving around in the game, just use that trick to check them. You can even hand-move objects while Paused, then Unpause (like normal, any changes in Play/Pause go away when you Stop.)
Then, in general, if something basic is acting funny, find a smaller program to test. Maybe just comment out lines from what you have, to find the smallest program where it breaks.
I have done all of that. While the scene is running, all of the coordinates stay where they started and where they should be, even unparented. I don't know how much more basic a program I can get. This doesn't do a whole lot besides check some positions and rotations. I'll see what I can do to dumb it down a little and then come back.
Alright, I managed to take out virtually everything except what was absolutely necessary and it still is giving me distances that do not make sense. When I cut it down, it was only two if statements, one was a position check, the other a tag check; a for loop so that it would find all the points it needed; a print statement; and a couple declarations. Nothing fancy.
Standard debugging is to keep printing, testing, until the problem is narrowed down to one line. Then you can look it up the command, figure out exactly what you think the line should go against what it really does, double-check every input and output on that line... .