Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Rangr · Dec 11, 2015 at 11:23 PM · c#positionstealthempty game objectcover

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!

alt text

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;
     }
 }
 
prefab-issue.png (66.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rangr · Dec 12, 2015 at 02:43 AM 0
Share

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.

avatar image Rangr Rangr · Dec 12, 2015 at 04:47 AM 0
Share

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.

avatar image Owen-Reynolds Rangr · Dec 12, 2015 at 06:22 PM 0
Share

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... .

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

36 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Children and parents in relative space 0 Answers

Position of empty game objects 1 Answer

Finding Boundaries of a Cover Object 1 Answer

Trying to Generate Different Random Values for Position of Game Object Instances [C#] 1 Answer

OnTriggerEnter and Exit using Position Markers? help?? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges