Rotating Derived Objects in hierarchy scales them? - unity3d

For an assignment I was told to experiment with the Unity hierarchy by making an amusement ride. I created a surface that moves up and down that works fine. When I add new objects to the surface that moves up and down and attempt to rotate them they also scale at the same time. I'm not sure why this happens. I have tried making them outside of the root object then adding them in but it does the same thing. If someone could give me a hint as to what I'm doing wrong that would be awesome.

If they scale right in the moment when you add them to the surface, then you probably scaled the surface before. In an object hierarchy all objects inherit the transformations of their parents.
You can avoid that by creating your tree with empty objects and only attach the visible objects as leafs.
Instead of doing it this way (which will result in the sub-objects beeing scaled according to the scale of the surface):
Surface
+SubObject 1
+SubObject 2
You can do it that way:
EmptyObject (scale 1)
+Surface
+SubObject 1
+SubObject 2
That should solve your problem

Related

My prefabs' size appears wrong when placed

So on a game I am working on, I keep having the same problem where whenever I turn an object into a prefab, somehow the object always appears smaller but the width and height says otherwise. I can't seem to fix this problem and it also applies whenever I try to instantiate it, it always remains that way even when I just create a prefab.
The Prefab as an example
The image I screenshot shows what happens whenever I assign or make an object as a prefab. Am I doing something stupid? or is it just because there is a setting I am not aware of?
In unity ths scale of an object affected by it's parent scale..
so for example if I have a cube with width 10 unit and it is scale is [10,1,1]
and I convert it to prefab every time Iniciate object from it will be in the same scale..
the problem apper if my cube was a chiled for other game object with scale for example [5,5,5]...
as you see in this picture
this is the same prefab but the size is totaly diffrent because of the parent
to avoid this proble be sure always to make your prefab with scale 1 1 1
you can create an empty game object with scale 1 1 1 and position[0,0,0] then make your object child of it then make your prefab ...
:) and if you want further help pleace give more specific informations for your problem

Render order according to hierarchy in Unity

I am trying to understand how Unity decides what to draw first in a 2D game. I could just give everything an order in layer, but I have so many objects that it would be so much easier if it was just drawing in the order of the hierarchy. I could write a script that gives every object its index, but I also want to see it in editor.
So the question is, is there an option that I can check so that it uses the order in the hierarchy window as the default sorting order?
From your last screenshot I could see you are using SpriteRenderer.
The short answer to the question "is there an option that I can check so that it uses the order in the hierarchy window as the default sorting order?" would be no, there isn't by default*.
Sprite renderers calculates which object is in front of others in one of two ways:
By using the distance to the camera, this will draw the objects closest to the camera on top of the rest of the objects in that same order in layer, as per the docs:
Sprite Sort Point
This property is only available when the Sprite Renderer’s Draw Mode is set to Simple.
In a 2D project, the Main Camera is set to Orthographic Projection mode by default. In this mode, Unity renders Sprites in the order of their their distance to the camera, along the direction of the Camera’s view.
If you want to keep everything on the same sorting layer/order in layer you can change the order in which the objects appear by moving one of the two objects further away from the camera (this is probably further down the z axis). For example if your Cashew is on z = 0, and you place the walnut on z = 1 then the cashew will be drawn on top of the walnut. If Cashew is on z=0 and the walnut is on z = -1 then the walnut will be draw on top (Since negative is closer to the camera). If both of the objects are on z - 0 they are both equally as far away from the camera, so it becomes a coin toss for which object gets drawn in front, as it does not take into account the hierarchy.
The second way the order can be changed is by creating different sorting layers, and adjusting the order in layer in the sprite renderer component. But you already figured that out.
*However, that doesn't mean it cannot be done, technically...
If you feel adventurous there is nothing stopping you from making an editor script that automates setting the order in layer for you based on the position in the hierarchy. This script would loop through all the objects in your hierarchy, grab the index of the object in the hierarchy, and assign the index to the Order in Layer.
I don't think Unity has such feature (https://docs.unity3d.com/Manual/2DSorting.html).
Usually you shall define some Sorting Layers:
far background
background
foreground
and assign Sprite Renderer of each sprite to one of Sorting Layers

.dae model disappears when approaching

When I move towards my .dae imported model, it disappears. I'm not "inside" the mesh yet, visibly at least, so I don't know what the deal is.
It looks like your object is closer than the scene-view camera's "Near Clip Plane", and is not being rendered as a result. The default editor "near clip plane" distance is around 0.3 units, so it shouldn't normally interfere with your objects.
Check that your object scale is correct. If your object is very small, the scene camera's near clip plane will seem much farther in comparison, and will appear to clip objects more aggressively.
You can create a default "Cube" primitive to check the size of your objects. Cubes are 1 unit in all dimensions by default, and most of the time it's a good idea to roughly map one unit to a real-world scale of 1 meter. If your object is considerably smaller than the cube, you may want to try scaling them up and seeing if that helps.
F key is a shortcut key that will automatically zoom and focus to an object. Select the GameObject and press F. This problem should be gone.
If the problem is still there, select the Camera and change the Clipping Planes Near to 0.3 and Far to 50000. You can mess with these values until Object stops disappearing. Although, pressing F should solve it.

Making multiple objects with the same shader fade at different times

I have a death transformation for one of my GameObjects which goes from a spherical ball to a bunch of small individual blocks. Each of these blocks I want to fade at different times but since they all use the same shader I cannot seem to figure out how to make all of them not fade out at the same time.
This first picture is the Spherical Ball in its first step for when it turns from a spherical ball to a Minecraft'ish looking block ball and to the right of it is one of the blocks that make up the Minecraft'ish looking ball shown by the red arrow.
Now this is my Inspector for one of the little blocks that make up the Minecraft'ish looking ball.
I have an arrow pointing to what makes the object fade but that is globally across all of the blocks since they use the same shader. Is it possible to have each block fade separately or am I stuck and need to find a new disappear act for the little block dudes?
You need to modify the material property by script at runtime, and you need to do it through the Renderer.material property. When you access Renderer.material, Unity will automatically create a copy of the material for you that is handled separately -- including getting its own draw call, if you care about performance. You can tell this has happened because the material name in the renderer will change to "Materialname (Instance)".
Set the material's fade property using Renderer.material.SetFloat() (or whatever the appropriate Set... function is). Unfortunately the property's name isn't "Fade Factor". You can find the property's name by looking at the shader script, or by switching the inspector to debug mode and digging through the Saved Properties array for one that looks right.

Roblox - Rotating a Model

I am working on a game which allows some buildings to be placed and
procedurally rotated.
I found the following rotation code which I am trying to incorporate:
function TransformModel(objects, center, new, recurse)
for _,object in pairs(objects) do
if object:IsA("BasePart") then
object.CFrame = new:toWorldSpace(center:toObjectSpace(object.CFrame))
end
if recurse then
TransformModel(object:GetChildren(), center, new, true)
end
end
end
which is invoked as follows in my building placement script:
local center = model:GetModelCFrame()
TransformModel( model:GetChildren(), center, center * CFrame.Angles(0,math.rad(45),0), true )
So the code works, somewhat... The model does sort of rotate but only after it jumps into the air and lands haphazardly before finally settling down in the rotated position, although its far from an exact 45 degree rotation after the physics effect of jumping in the air and settling back down.
I am not quite sure but I suspect there is a better way to accomplish this where the model smoothly rotates. Any help would be greatly appreciated.
Thanks,
Andy
GetModelCFrame is a deprecated method. It is instead recommended to utilize the PrimaryPart, GetPrimaryPartCFrame, and SetPrimaryPartCFrame. Setting the primary part designates a specific part in the model to serve as a base that you can move around via its CFrame. What's nice about this approach is that if you transform the PrimaryPart of a Model with these methods, all of the other parts in the Model will also transform to maintain their offset from the PrimaryPart.
For placing buildings and props, it is usually desired to have this part be a flat part at the bottom that represents the footprint of the building.
For example, consider the following model:
In this case the best part to make the primary part would be the one on the bottom. If your model doesn't have a part incorporated in the geometry then you can make an invisible and cancollide false part to serve as the base. In the following example, there is a tree model that doesn't have a wide base, but we want to make sure the footprint is large enough that it wouldn't overlap with other nearby objects:
In this case the yellow part should be made fully transparent at runtime.
So in terms of the code to actually orient a model in the way you want, all you have to do is call SetPrimaryPartCFrame with the new CFrame. The TransformModel function simply becomes:
local function TransformModel(model, newCFrame)
model:SetPrimaryPartCFrame(newCFrame)
end
When you want to call this function to say rotate a model 45 degrees:
local model = game.Workspace.Model
model.PrimaryPart = model.Base
local rotatedCFrame = model:GetPrimaryPartCFrame() * CFrame.Angles(0, math.rad(45), 0)
TransformModel(model, rotatedCFrame)
I believe I found an answer that works for my problem, though I am not sure if this is the best way to fix it. I created an extra part to act as a base which is the exact width and height of the model, the model then sits on top of the new base part and the base is anchored. That seems to have fixed it - is there a better way?
Now, GetPrimaryPartCFrame and SetPrimaryPartCFrame are not recommended for future projects. Instead, it is best to use SetPivot() and GetPivot().
(I understand that this topic was quite a while back ago, so I'm saying this to people who stumble upon this)