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() {
StartCor
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".
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.