0
\$\begingroup\$

I have in the Hierarchy a empty GameObject and a cube as child of it. The cube the child is at position 0,0,0 and scale 1,1,1

But when running the game the scaling is on both sides of the cube. I put the cube as child of the empty gameobject and scaling the empty gameobject but still it's scaling the cube on both sides.

The script is attached to the empty GameObject:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Walls : MonoBehaviour
{
    Vector3 originalScale;
    Vector3 destinationScale;

    void Start()
    {
        originalScale = transform.localScale;
        destinationScale = new Vector3(100.0f, 0, 0);     
    }

    private void Update()
    {
        if (transform.localScale.x != 100.0f)
            transform.localScale += new Vector3(1, 0, 0);
    }
}

This is a working solution:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Walls : MonoBehaviour
{
    public Transform[] objectstoScale;

    private void Update()
    {
        if (objectstoScale[0].localScale.x != 100.0f)
        {
            objectstoScale[0].localScale += new Vector3(1, 0, 0);
        }
    }
}

But i want to make that the same cube will also scale one side on the z. So the cube will be scaled on both x and z but without filling it one be like frames. So i tried this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Walls : MonoBehaviour
{
    public Transform[] objectstoScale;

    private void Update()
    {
        if (objectstoScale[0].localScale.x != 100.0f)
        {
            objectstoScale[0].localScale += new Vector3(1, 0, 0);
            objectstoScale[1].localScale += new Vector3(0, 0, 1);
        }
    }
}

And i have now two cubes childs of the empty gameobject one cube position is 0.5,0,0 the second is 0,0,0.5 but what i'm getting is:

Scale

What i want to do is to create automatic a rectangle scaling a cube on 4 sides at the same time not filled rectangle.

For example like this:

Rectangle

\$\endgroup\$
0
\$\begingroup\$

Position the child cube at (0.5, 0, 0) in its parent's coordinate space. That will put one side exactly on the origin of the parent at its default scale (the cube measures 1x1x1 by default, so it extends one half a unit in each direction)

Then scale the parent by your desired factor. Since one side is exactly at the origin/zero, and zero times any scale factor is still zero, that side will stay put and the other side will move.

Alternatively, you can reposition the cube by half its scale factor to keep your desired side at the origin.

\$\endgroup\$
  • \$\begingroup\$ This is working i used the first solution changing the position to 0.5 and now what do i do if i want to make the same cube or maybe i need another cube from the same position but so scale it only on the z this time ? So both cubes or the same cube will be scale on z and x but without filling the cube ? \$\endgroup\$ – user1196715 Oct 10 '17 at 21:51
  • \$\begingroup\$ Try working out the math for yourself. It's not complicated. Just get the sides you want to stay in place to be at zero on their respective dimensions (eg. if I want the x- and z- faces to stay in place as I scale in the positive x and z directions, I'd move the inner cube to (0.5, 0, 0.5)) \$\endgroup\$ – DMGregory Oct 10 '17 at 21:53
  • \$\begingroup\$ In the editor i changed the cube position to 0.5,0,0.5 in the script i changed the line to objectstoScale[0].localScale += new Vector3(1, 0, 1); but then it's making a filled rectangle. It's scaling x and z but filled. \$\endgroup\$ – user1196715 Oct 10 '17 at 22:00
  • \$\begingroup\$ Are you trying to make an L shape? Scaling won't turn a rectangle into an L, you need to use two cubes or a custom mesh. \$\endgroup\$ – DMGregory Oct 10 '17 at 22:04
  • \$\begingroup\$ I did it with two cubes but also with two empty gameobjects just duplicated the empty gameobject with the child cube and changed a bit the script and in the inspector one cube 0.5,0,0 second 0,0,0.5 and in each empty gameobject i changed the scale 1,0,0 and 0,0,1 i made global vector3 variable. \$\endgroup\$ – user1196715 Oct 10 '17 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.