- Home /
Change the size of an object, but only one side
Hello and thank you,
When I used to program in XNA in 2d,I could draw an image using a rectangle with the inputs of x, y, height, width.
Now, using unity, if I shrink the Y scale, it takes pixels off both sides of the object. This makes the object appear to raise up.
Is there a way to shrink an object, but just from the top?
The object has a sprite renderer...not sure if that answers the question. Using the transform component.
Please DO NOT post comments as answers. Does ^-this answer the question? NO! So why is it an answer? If you don't know how to use this site, watch the tutorial video on the right and read the FAQ.
Answer by Ashish Dwivedi · Feb 08, 2014 at 03:44 PM
using UnityEngine;
using System.Collections;
public class Turn : MonoBehaviour
{
float mfX;
float mfY;
// Use this for initialization
void Start ()
{
mfX = transform.position.x - transform.localScale.x/2.0f;
mfY = transform.position.y - transform.localScale.y/2.0f;
}
// Update is called once per frame
void Update ()
{
if(Input.GetMouseButton(0))
{
Vector3 v3Scale = transform.localScale;
transform.localScale = new Vector3(v3Scale.x + 0.1f , v3Scale.y + 0.1f , v3Scale.z);
transform.position = new Vector3(mfX + transform.localScale.x / 2.0f, mfY + transform.localScale.y / 2.0f , 0);
}
}
}
Add this script to your object and press mouse left button and you will get what ever you want. You can change this according to your requirements. I hope this would help you...
Your answer

Follow this Question
Related Questions
Every active Object a Script? no some efficient way? 4 Answers
Polymorphism and interfaces 2 Answers
trying to update the guiText.text component only when required 0 Answers
How exactly do Monobehaviour functions work? 2 Answers
Duplicating GameObject with variations of the same functionality. 0 Answers