How can i scale a cube from one side only on x only ?
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);
}
}
Answer by bobisgod234 · Oct 10, 2017 at 10:49 PM
Offset the cubes local position by 0.5 on the X axis, then you should be able to scale the parent along the X axis and get the effect you want.
Answer by junedmmn · Sep 04, 2018 at 03:19 PM
@Chocolade, 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".