- Home /
Thanks to HarshadK.
Zoom in/out Object
Hi, i have to zoom in/out my object(not camera) using pinch with min and max values.
using UnityEngine;
using System.Collections;
public class Zoom_Button : MonoBehaviour
{
private Transform Table_Transform = null;
private float Scale_Factor = 0.5f;
private bool Transform_Update = false;
public GUITexture Zoom_In, Zoom_Out;
// Use this for initialization
void Start () {
GameObject Table = GameObject.FindWithTag("Table");
if (Table != null)
{
Table_Transform = Table.transform;
}
}
// Update is called once per frame
void Update () {
if (Transform_Update) {
Table_Transform.localScale =
new Vector3(Scale_Factor, Scale_Factor, Scale_Factor);
Transform_Update = false;
}
if(Input.touchCount > 0)
{
if(Zoom_In.HitTest(Input.GetTouch(0).position))
{
ScaleUp();
}
if(Zoom_Out.HitTest(Input.GetTouch(0).position))
{
ScaleDown();
}
}
}
void OnGUI()
{
Vector3 scale;
float ResolutionX = 1280;
float ResolutionY = 728;
scale.x = (float)Screen.width/ResolutionX;
scale.y = (float)Screen.height/ResolutionY;
scale.z = 1;
Matrix4x4 svMat = GUI.matrix;
GUI.matrix = Matrix4x4.TRS(new Vector3(0,0,0),Quaternion.identity,scale);
}
void ScaleUp() {
if (Table_Transform != null) {
Scale_Factor = Table_Transform.localScale.x;
Scale_Factor *= 1.5f;
Transform_Update = true;
}
}
void ScaleDown() {
if (Table_Transform != null) {
Scale_Factor = Table_Transform.localScale.x;
Scale_Factor *= 0.67f;
Transform_Update = true;
}
}
}
Right now am using this code to zoom in/out object, yes its working with button but i have to add min and max value for my zoom in/out and importantly i need a pinch zoom. I don't know how to add those.
What you are using for augmented reality? Vuforia or some other plugins.
Answer by HarshadK · Dec 01, 2014 at 08:42 AM
This tutorial on PINCH TO ZOOM does all the things you want but is for camera. You just need to adapt it to work with your current object scale instead of camera FOV.
There is also script available for that tutorial under the video. :-)
@Harshad$$anonymous$$, can you please explain how to change those scale factor ins$$anonymous$$d of field of view.
Here's the updated script to work with scale changing:
public float scaleFactor = 0.5f; // The rate of change of the scale
public float $$anonymous$$Scale = 0.6f;
public float maxScale = 2.0f;
void Update()
{
// If there are two touches on the device...
if (Input.touchCount == 2)
{
// Store both touches.
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
// Find the position in the previous frame of each touch.
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
// Find the magnitude of the vector (the distance) between the touches in each frame.
float prevTouchDelta$$anonymous$$ag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDelta$$anonymous$$ag = (touchZero.position - touchOne.position).magnitude;
// Find the difference in the distances between each frame.
float delta$$anonymous$$agnitudeDiff = prevTouchDelta$$anonymous$$ag - touchDelta$$anonymous$$ag;
// Change the scale based on the change in distance between the touches.
Table_Trasnform.localScale.x += delta$$anonymous$$agnitudeDiff * scaleFactor;
Table_Trasnform.localScale.y += delta$$anonymous$$agnitudeDiff * scaleFactor;
Table_Trasnform.localScale.z += delta$$anonymous$$agnitudeDiff * scaleFactor;
// Clamp the scale factor to make sure it's between $$anonymous$$Scale and maxScale.
Table_Trasnform.localScale.x = $$anonymous$$athf.Clamp(Table_Trasnform.localScale.x, $$anonymous$$Scale, maxScale );
Table_Trasnform.localScale.y = $$anonymous$$athf.Clamp(Table_Trasnform.localScale.y, $$anonymous$$Scale, maxScale );
Table_Trasnform.localScale.z = $$anonymous$$athf.Clamp(Table_Trasnform.localScale.z, $$anonymous$$Scale, maxScale );
}
Just get the concept and you'll be good to go.
Thanks for your help, i tried your code. But having some value type error. This is the error.
"Cannot modify a value type return value of `UnityEngine.Transform.localScale'. Consider storing the value in a temporary variable"
I get it, i cleared the error, and thanks for your help.
@Harshad$$anonymous$$, i understand your logic. But your code doesn't work for me.
What is happening? What behavior to you get? You also need to change the values for various variables like scaleFactor, $$anonymous$$Scale, and maxScale also.
Follow this Question
Related Questions
Cloud recognition in Vuforia 0 Answers
Projection mobile camera on a texture, at the same time using it as AR camera. Is it possible? 0 Answers
How to prevent image from rendering to screen but seen by camera 0 Answers
object projection from one camera to another 1 Answer
is there any tricks while navigating the scenes with the camera 1 Answer