- Home /
How to change the scaling of a gameobject during runtime?
Hi All
I want to change the size of gameobject as a measuring tape. Suppose if I touch and drag an object it should the increase the size of the gameobject in the specified direction.
Is this possible?
Can anyone please help me regarding this?
Answer by robertbu · Sep 20, 2013 at 04:45 AM
If the camera is axes aligned, then dragging out a Quad is relatively easy. Create a new scene. Create a Quad (GameObject>Create Other>Quad). Attach the following script:
#pragma strict
private var pos1 : Vector3;
private var pos2 : Vector3;
function Update () {
if (Input.GetMouseButtonDown(0)) {
pos1 = Input.mousePosition;
pos1.z = transform.position.z - Camera.main.transform.position.z;
pos1 = Camera.main.ScreenToWorldPoint(pos1);
transform.localScale.x = 0;
}
if (Input.GetMouseButton(0)) {
pos2 = Input.mousePosition;
pos2.z = transform.position.z - Camera.main.transform.position.z;
pos2 = Camera.main.ScreenToWorldPoint(pos2);
var v3 = pos2 - pos1;
transform.localScale.x = v3.magnitude;
transform.position = pos1 + v3 * 0.5;
transform.rotation = Quaternion.FromToRotation(Vector3.right, v3);
}
}
Yeah thanks robertbu but I'm having a doubt in this code. Actually I want to increase the size of the scale in the x axis right side alone but sometimes its rotating please suggest ny idea please
Assu$$anonymous$$g the ruler is horizontal, if you make pos2.y = pos1.y, then you will only get a horizontal ruler. This does not solve your 10cm and only grow on the right, but it does keep the ruler from rotating.
Answer by nur farazi · Jun 01, 2014 at 05:12 PM
you can use drag code , and put a scale function , here is the code how you can use the drag function in your script , now just put a scaling funtion like (http://docs.unity3d.com/ScriptReference/Vector3.Scale.html) using UnityEngine; using System.Collections;
public class Drag2 : MonoBehaviour {
private float dist;
private bool dragging = false;
private Vector3 offset;
private Transform toDrag;
void Update() {
Vector3 v3;
if (Input.touchCount != 1) {
dragging = false;
return;
}
Touch touch = Input.touches[0];
Vector3 pos = touch.position;
if(touch.phase == TouchPhase.Began) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(pos);
if(Physics.Raycast(ray, out hit) && (hit.collider.tag == "Draggable"))
{
Debug.Log ("Here");
toDrag = hit.transform;
dist = hit.transform.position.z - Camera.main.transform.position.z;
v3 = new Vector3(pos.x, pos.y, dist);
v3 = Camera.main.ScreenToWorldPoint(v3);
offset = toDrag.position - v3;
dragging = true;
}
}
if (dragging && touch.phase == TouchPhase.Moved) {
v3 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
v3 = Camera.main.ScreenToWorldPoint(v3);
toDrag.position = v3 + offset;
}
if (dragging && (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)) {
dragging = false;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613114513im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I make a gameobject to scale in a single direction? 2 Answers
Ragdoll 2D character controller help 0 Answers
Touch and Drag for Android problem 0 Answers
How to force Eventsystem Drag 1 Answer