简体   繁体   中英

Unity: Scale a GameObject in world space

I am trying to achieve this:

在此处输入图片说明

This is a non-axis-aligned cube being scaled in world space in blender alongside the Z axis (Y in Unity). I want to achieve the same in Unity. I tried to find formulas for this but had no luck, I know that I can parent my object to another GameObject and scale the parent, but once I unparent my original GameObject the scale resets back and I need to scale the original GameObject. Any advise would be much appreciated.

You can either unparent it, scale it and then reparent it back, or you can create game object that will hold both these objects separately so they all have their own scale.

void Rescale(Transform obj, Vector3 newScale)
{
    if(obj.root != obj)
    {
        Transform parent = obj.parent;
        obj.SetParent(null);
        obj.localScale = newScale;
        obj.SetParent(parent, true);
    }
}

You can transform a scale (Vector3) from world space to local space by using this function:

transform.parent.InverseTransformVector(worldScale)

To apply to your cube the correct scale you might do something like this:

cube.transform.localScale = cube.transform.parent.InverseTransformVector(worldScale);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM