- Home /
Set Colliders at same size
I am working on a system of make the player cover in a wall(the system of cover is already done), so I have a wall that inside of that object it has 4 empty objects that are set in the 4 edges of the rectangle wall and these 4 empty objects have a box collider that are trigger and all is fine until I decide to scale the wall bigger or smaller, the 4 empty objects grow too and I want that when you scale it the empty objects change the position but not the scale. I want an easy idea with no use of script but if is not possible, with script is ok.
Answer by aldonaletto · Aug 20, 2011 at 07:49 PM
There's no way to keep the original size without scripts when you change the parent scale at runtime. Calculate the original object scale (it's **initial scale parent scale* - the function Vector3.Scale does this multiplication), then anytime the parent scale changes, divide each original scale component (x, y, z) by its corresponding in the new parent scale.
private var orgScale: Vector3; // original child scale before parenting private var lastScale: Vector3; // last parent scale
function Start () { lastScale = transform.parent.localScale; // calculate the original child scale (localScale * parentScale) orgScale = Vector3.Scale(transform.localScale, lastScale); }
function Update () { var dadScale = transform.parent.localScale; if (dadScale != lastScale){ // if daddy changed scale... var newScale = orgScale; // calculate new child scale: newScale.x /= dadScale.x; newScale.y /= dadScale.y; newScale.z /= dadScale.z; transform.localScale = newScale; // set child scale at once lastScale = dadScale; // remember last parent scale } }
Answer by V-Jankaitis · Jan 12, 2017 at 04:36 PM
if (Size != Old_Size && SphereColider != null)
{
SphereColider.radius *= Old_Size / Size;
}
transform.localScale = new Vector3(Size, Size, Size);
Old_Size = Size;
important part is "*= Old_Size / Size;"
Your answer
![](https://koobas.hobune.stream/wayback/20220612185009im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Permanent space between objects 2 Answers
Can you scale a Mesh independently of it's Collider? 1 Answer
Wheelcolliders bug ? 1 Answer
Positioning gameobjects by a corner, not by center 0 Answers