3

I have added Cube to scene and I scaled and put to position ( 0, 0, 0 ). I am scaling that Cube with code attached to Cube

IEnumerator Start() {
    StartCoroutine("DoSomething", 2.0F);
    yield return new WaitForSeconds(1);
    StopCoroutine("DoSomething");
}

IEnumerator DoSomething(float someParameter) {
    while (true) {
        transform.localScale += new Vector3(0, 0.1f, 0);
        yield return null;
    }
}

but Cube scales on both sides, to top and bottom. I want to scale with same factor but that bottom of Cube stays on same position. I tried to set new position between transform.localScale += new Vector3(0, 0.1f, 0); and yield return null; but I don't know how to get exact amount. ( I tried to read

Mesh planeMesh = gameObject.GetComponent<MeshFilter>().mesh;
        Bounds bounds = planeMesh.bounds;
        bounds.size.y;

before and after scale and add to

transform.position.y difference between boundsAfter.size.y and boundsBefore.size.y

but it moves too much.

How to solve this ?

Programmer
  • 121,791
  • 22
  • 236
  • 328
PaolaJ.
  • 10,872
  • 22
  • 73
  • 111
  • You problem is that you only want to scale the cube on one side? only in the up direction not up and down when scaling? – Programmer Aug 29 '16 at 23:13
  • @Programmer Yes, for example, if Cube bottom is at 0 on y coordinates after scale I want to stay on 0 on y coordinates. At the moment it scales but bottom of the Cube goes to less than 0 on y coordinates. – PaolaJ. Aug 29 '16 at 23:18

2 Answers2

8

You can actually do this in two ways.

METHOD 1:

Change pivot point from an external application.

Create a simple Cube in a 3D software such as Maya then position the pivot point to the bottom of the cube and then export it Unity. This should work out of the box.

METHOD 2:

Parent the Cube to an empty GameObject.

Steps:

A.Create a an Empty GameObject.

B.Move the new GameObject to the position of the Cube then move the y axis of that new GameObject to the bottom of the cube. The y-pos must be positioned precisely by zooming in. See to the animation below for more information.

C.Now drag the Cube to the that new GameObject and the cube should be the child of that new GameObject. You can now attach the script in your question to that new GameObject and it should scale the cube on one side only. Don't forget to remove the old scaling script that is directly attached to the cube.

Image Tutorial. High quality version here.

enter image description here

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Can you explain what's happening in Method 2? Why is it now scaling on one side only? I ask because I'm trying to scale both sizes procedurally, and can't use two parent objects haha – Matt Voda Jul 14 '18 at 03:16
  • 1
    @MattVoda You are scaling a parent object so the scale will affect the child object based on the position of the parent object – Programmer Jul 14 '18 at 06:10
  • Does the parent's transform effectively create a new pivot point for the child object (like mentioned in Method 1)? – Matt Voda Jul 16 '18 at 18:23
  • Nope. It simply changes the local point to rotate, scale the child objects. To change the actual pivot you have to use method 1. – Programmer Jul 16 '18 at 19:00
-1

You can use Slider for the purpose. I was designing a Loading bar for which I needed to Increase width of the loader according to the download that is progressed. You can disable its handle, and use custom Sprite for loading fill percentage. You can also use custom Sprite for background. To use Slider, go to Menu Bar -> GameObject -> UI -> Slider. Or instead You can Add a Slider Component from the Inspector for the GmaeObject you want (Select the GameObject you want to add Slider -> In the Inspector click Add Component -> UI -> Slider. Now Set Fill Rect and other Options). You can set Slider from Left to Right, Right to Left, Bottom to Top, or Top to Bottom.

In the Script

using UnityEngine.UI;
public class SliderExample : MonoBehaviour
{
    public Slider slider;  // Drag and Drop Slider GameObject in Unity Inspector
    float percentageCompleted;

    void Update()
    {
        slider.value = percentageCompleted;  
        // specify percentage Completed, if required Divide by 100 as Slider values are from 0 to 1 or set Max Value in Inspector as 100
    }
}

You can also watch this Brackeys video for some help regarding setting up the Slider "https://www.youtube.com/watch?v=YMj2qPq9CP8".

Junaid Pathan
  • 3,850
  • 1
  • 25
  • 47