Unity 3D. Child object does not change

I am a Java programmer and I am new to Unity. I am writing a grid that lies on a Plane object. The goal is to decouple the grid-based logic from the Unity 3D coordinate system. The game is actually talking about the position of the grid, so I need some code that converts the game's coordinate system to / form's Unity coordinate system (world position). Since the position of the Plane object can be changed (rotated, moved, etc.), I want to "mark" the lower left corner of the plane. In this case, I will be able to convert one coordinate system to another at any time, since from the point of view of the game, the "marker in game" coordinate is 0.0 (2D). Unfortunately my code doesn't work.

Could you tell me what is wrong (the "marker" properties do not change when the 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;                                                           
    }
 }

      

+3


source to share


2 answers


I am assuming the getGround () method is returning the wrong ground. Setting the marker .transform.parent = ground.transform must be absolutely sure that the marker position is updated with the ground.

On a side note, it might be more legible if you 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;        

      

This way you don't mess with the math Unity does.

0


source


I found several problems in your code:

  • if the marker is an empty GameObject, you don't need to scale it.
  • If it is not an empty GameObject, but you must not be visible inside the game, than set the scale to a very low value, but greater than zero.
  • If you are using Unity 5 or higher I would recommend you a SetParent

    method to assign a parent to your ie markermarker.transform.SetParent(ground.transform);

  • First assign a parent to your marker, then transform it (position and rotation).


If you first change the transform and then assign a parent to it, then what kind of unity it would do is to put the marker at the "Assigned Position" in world coordinates, and then, by assigning it to the parents, it will compute the equivalent position in the parent space wrt world space, but in As a result, the object will look the same as it did before the parent was added to it.

eg. If the parent has a scale of 0,5,0,5,0.5, and the child (before the birth of the child) has a scale of 1,1,1. therefore, when the child has now MAKED THE CHILD of parental unity, he will update his scale to 2.2.2. So the size changes, but it looks the same inside the game. The same goes for position and rotation.

0


source







All Articles