Hi all,
I wish to make a scrollbar that can scale the object I chose in respect to the handle’s position.
The job is done, but I have one problem: no matter if I have the object in mention’s pivot in the center, it gets scales from the corner. (it’s a game object containing multiple game objects but that shouldn’t be a problem).
I’m using transform.localScale by the way. All the help appreciated.
The editor’s scale tool does the job right, however I don’t know how to access that from script to change its value.
@agiro
You did not give any code or much to go on (Is this 2D/3D, are you scaling an UI.Image, SpriteRenderer or other type of object - e.g. 3D Cube).
But the following script will handle it all (note it uses a UI slider object in place of the scroll bar)
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScaleObject : MonoBehaviour
{
public Slider slider;
public GameObject objectToScale;
public float maxScale = 5; // max scalue value of objectToScale
public float defaultScale = 1; // scale value for objectToScale to start at
void Start()
{
if (objectToScale == null)
{
Debug.LogError("objectToScale has not been set in the inspector. Please set the objectToScale and try again.");
}
else
{
if (slider == null)
{
Debug.LogError("Slider has not been set in the inspector. Please set the Slider and try again.");
}
else
{
slider.onValueChanged.AddListener(ScaleImageListener);
slider.value = defaultScale / maxScale; // set the scale of objectToScale to its default scale
}
}
}
void ScaleImageListener(float value)
{
float scaleValue = maxScale * value;
objectToScale.transform.localScale = new Vector3(scaleValue, scaleValue, 1);
}
}