3
votes

I'm java programmer and I'm new with Unity. I'm writing a grid that lays on a Plane object. The aim is to separate grid-based logic from Unity 3D coordinate system. Actually the game speaks in terms of grid positions, so I need a code that converts game coordinate system to/form Unity coordinate system (World position). Since the position of Plane object may be modified (rotated, moved, etc), I want to "mark" Plane left bottom corner. In this case I will be able to convert one coordinate system to another at any time, since form the game point of view the "marker in game" coordinates is 0,0 (2D). Unfortunately my code doesn't work.

Could you please say me what is wrong (the properties of "marker" are not changed when Plane is moved)?

public class GridManager: MonoBehaviour {

    GameObject ground;
    GameObject marker;

    ...

    void attachMarker() {

        //get substrate (plane)
        GameObject ground = getGround();                                            

        //create marker
        GameObject marker = new GameObject();                                       

        //set marker position to left bottom corner 
        marker.transform.position.x = ground.transform.position.x - ground.transform.localScale.x/2;     
        marker.transform.position.z = ground.transform.position.z - ground.transform.localScale.z/2;    
        marker.transform.position.y = 0;

        //set marker size
        marker.transform.localScale = new Vector3(0,0,0);   

        //set marker name (will be used for search) 
        marker.name = "LB_Marker";      

        //set marker to be child of ground                              
        marker.transform.parent = ground.transform;     

        //store it
        this.marker = marker;                                                           
    }
 }
2
Why are you scaling your marker to 0,0,0? - joreldraw
Your code seems esentialy ok. How are you moving the plane? Are you checking the marker properties while the project is running? - xpereta
1. Why are you scaling your marker to 0,0,0 From my point of view marker is point. I have no problem with getting it position. 2. Are you checking the marker properties while the project is running? Yes. By using debug print and test script that change Plane position on Update. - bw_dev
First set the parent and later the scale and position. - joreldraw
Please, don't use Unity tag for questions related to Unity game engine. - Max Yankov

2 Answers

0
votes

My guess is that the getGround() method is returning the wrong ground. Setting the marker.transform.parent=ground.transform should make absolutely sure that the marker position gets updated with the ground's.

On a side note, it might be more legible if you instead say:

//get substrate (plane)
GameObject ground = getGround();                                            

//create marker
GameObject marker = new GameObject();      
marker.transform.parent = ground.transform;
marker.transform.localPosition = new Vector3(0.5f, 0.5f, 0);

//set marker name (will be used for search) 
marker.name = "LB_Marker";
//store it
this.marker = marker;        

That way, you're not messing with doing math that Unity is already doing.

0
votes

I found a few problems in your code:

  1. if the marker is an empty GameObject than you don't need to change its scale.
  2. If it is not an empty GameObject but you don't to be visible inside the game than set the scale to a very low value but greater than zero.
  3. If you are using Unity 5 or above I would recommend you SetParent method to assign a parent to your marker i.e. marker.transform.SetParent(ground.transform);
  4. First assign a parent to your marker then play with its transform (position and rotation).

If you first change the transform and then assign a parent to it, what unity will do is place your marker at the "Assigned position" in world coordinates and then when assigning it to the parent it will calculate the equivalent position in parent space w.r.t world space coordinate but as a result the object will look the same as it looked before being added to it parent.

e.g. If the parent has a scale of 0.5,0.5,0.5 and the child (before making it a child of parent) has a scale of 1,1,1. so when the child is now MADE A CHILD of parent unity will update its scale to 2,2,2. Thus, the size changes but it looks the same inside the game. Same goes for position and rotation.