3

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;                                                           
    }
 }
bw_dev
  • 775
  • 1
  • 7
  • 17
  • Why are you scaling your marker to 0,0,0? – joreldraw May 05 '15 at 14:30
  • Your code seems esentialy ok. How are you moving the plane? Are you checking the marker properties while the project is running? – xpereta May 05 '15 at 14:34
  • 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 May 05 '15 at 14:43
  • First set the parent and later the scale and position. – joreldraw May 05 '15 at 14:44
  • 1
    Please, don't use `Unity` tag for questions related to Unity game engine. – Max Yankov May 05 '15 at 15:34
  • Why do you subtract scale from coordinate? I don't know what are you trying to do in this line, but it doesn't make any sense at all. It's like kilograms and newtons: if you end up with adding one to another, you can be sure something is wrong. – Max Yankov May 05 '15 at 15:38
  • Also, `marker.transform.x` shouldn't even compile, the `Transform` class doesn't have `x` member. You meant `maker.transform.position.x`, right? – Max Yankov May 05 '15 at 15:38
  • And finally, I don't understand why do you need to translate coordinate systems into one another at all. Just use local positions, rotations and scale, they are defined relative to the parent: that way, you'll be able to move, scale and rotate parent however you want, and local coordinates wouldn't change at all. – Max Yankov May 05 '15 at 15:39
  • Sorry. I will not use it. – bw_dev May 05 '15 at 15:58
  • The game logic uses 2 dimensions array to store states and data (think about chess). What I need is: a. translate user selection (mouse click or mouse over, etc) to indexes in this array. These indexes are the game system of coordinate (again - chess like game with really heavy logic) b. translate indexes into Unity 3d coordinates for visualization. If logic decided to move object form tile (1,3) to tile (7,10) I need to know Unity coordinates of appropriate tiles. The grid is an isolation layer between logic and visualization. – bw_dev May 05 '15 at 16:35
  • Thanks to joreldraw :) I changed order and now it works. – bw_dev May 06 '15 at 10:50

2 Answers2

0

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

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.

Anas iqbal
  • 1,036
  • 8
  • 23