Search Unity

Question y position of objects

Discussion in 'Editor & General Support' started by kmay11005, Aug 31, 2020.

  1. kmay11005

    kmay11005

    Joined:
    Aug 30, 2020
    Posts:
    2
    I'm new to unity and recently started making a simple game based off of Brackey's tutorials. I've noticed that some of my objects (all have a y position of 1) aren't on the ground (some of them are overlapping with the ground). I have the ground at a scale of 25x1x1000 and a position at 0,0,0. I thought maybe the engine starts measuring the position from the center of the object, so for example, a 3x3x3 would have a y position of 2.5 (1.5 because of the distance from the center of the object to the ground + 1 for the height of the ground) to be on the ground.
    After some experimenting, I've figured out that objects with a scale of 3x3x3 need a y position of 2 to be exactly on the ground, ones with a scale of 5x5x5 need a y scale 3 to be exactly on the ground, and a 1x1x1 should have a y scale of 1 to be on the ground. How does this system work and where do these numbers come from?
    Also, I tried to update the y position of all my game objects through their prefabs but that didn't do anything. Does this mean that the position of a prefab doesn't affect the objects because each of the individual objects are overriding the prefab? And is there a better way to fix the positions of game objects than to manually do so?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    This is the case for all of Unity's primitive shape meshes (Cube, Cone, Sphere, etc...). The reality is that each 3D object you import into Unity comes with its own pivot point, which is a property of the 3D model itself. Unity respects and uses whatever the pivot point of the imported mesh is. As you noticed, for the primitive builtin Unity shapes, that pivot point is the geometric center of the object. It's also common for people to make 3d objects whose pivot point is the "bottom" of the object. For example a tree or a light pole. The point of this is pretty much to make it easy to place that object on a surface and avoid the kind of issue you're experiencing here.

    It's exactly as you've noticed. Basically your "ground" object has an overall height of 1, has its pivot point in its geometric center, and is sitting at (0,0,0). That puts the top surface of that object at height 0.5. The numbers just come from those three things:
    • Where is the pivot point of the object?
    • What is the position of the object in the world? (The coordinates you see in the inspector, assuming it's a root object in the hierarchy)
    • What are the vertical extents/overall size of the object?
    You may find it convenient to place your ground object at (0, -0.5, 0) if the height is 1. That will put the top surface of it at exactly 0.

    To change the pivot point of an object you can either create your own custom 3D model via external software like Blender, or through a plugin like ProBuilder. Alternatively you can parent the object to an empty GameObject and offset it from the empty parent by the appropriate amount.

    Whenever you instantiate a prefab in a script, you have to provide a position and rotation for the new object. So yes, the position of the actual prefab in the prefab editor is pretty much meaningless, and should almost always be set to (0,0,0).