- Home /
Forcing all new GameObjects to a specific Position z-value
Hello everyone! When dragging/dropping a prefab or GameObject to the scene, it tends to have a random z-value. I was wondering if it was possible to force every prefab, GameObject, etc. that is dragged from the Project view to the Heirarchy or Scene to have a specific z-value for Position. Is this possible?
FYI, this is for a 2D game, where the z-value determines where an object is in front of or behind other objects (so having a consistent "starting point" goes a long way).
Yes, I mean while working in the editor. Doing it to the rigidbody unfortunately will not work for me. Any tips on what I should be trying to do in an editor script to get this to work?
Answer by sparkzbarca · Oct 16, 2012 at 06:40 PM
Well if its a rigid body I think you can possibly set the z axis in the prefab to zero. and then in the prefab lock Z axis. That might force all prefabs dragged to keep the prefabs Z axis of zero (or whatever number)
the constraints are under rigidbody. I myself dont know of anyway to put constraints on just a transform. At least not in the editor, could you script one up easy but it'd only go into affect once you began the game.
I checked that and nope it doesnt work, it only works if you drag it into the hierarchy not the scene.
Unfortunately the objects in question are not rigid bodies. Is there any way to set the z-axis using Editor Scripts?
var zPos : float = 0.0;
function Update() { gameObject.transform.position.z = zPos; }
that way you have control in the component. I imagine it would force it in the value set in the zPos... but i don't think this is ideal.. there should be a way to lock $$anonymous$$ and max values in Z in edtiro preferences or something.. when dropping prefabs in scene.. =\
Is there a concept in Unity of editor scripts that are always running in the background? If so, I can write an editor script that watches events on the Project tab and whenever something is dragged and dropped I can change it's z-index. Is that possible?
I've got an idea...
http://docs.unity3d.com/Documentation/ScriptReference/DragAndDrop-objectReferences.html
get object name being dragged and dropped and then..
http://docs.unity3d.com/Documentation/ScriptReference/ScriptableObject.OnEnable.html
refer to that script getting the object and applying a certain value to the z axis, of that object.
Answer by WarmBiscuit · May 25, 2017 at 02:52 PM
The objects that you're dragging from the Prefab and placing into the scene are going to have a Z-Value relative to the original Z-Value of the Prefab. In order to get everything on the same Z-Value plane, you need to make sure to "Reset" the position of the Prefab before dragging items into the scene from it.
All the items will have different Z-Values (as well as X and Y values) depending on what the coordinate values of the original Prefab are. So make sure to reset your Prefabs to the origin before dragging objects from it into your scene.
Your answer
Follow this Question
Related Questions
Change default sprite position when dragged into Hierarchy 1 Answer
in-editor cloning of game objects 1 Answer
I want to move a cube with rotation but I find this problem 1 Answer
Get the name of an instance's prefab at runtime? 0 Answers
Physics.OverlapSphere() not working after using pool of objects 0 Answers