- Home /
Scale / Scaling Object using touch on Android
Does anyone know what the script should look like to be able to Scale the gameobject using 2 fingers touch on an Android device?
I used 2 scripts to do this (please see below), but it does not work unfortunately. What changes do I have to make to be able to scale my object?
Script 1: I used this script and put it in an empty gameobject:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CSharpscaling : MonoBehaviour
{ public float initialFingersDistance;
public Vector3 initialScale;
public static Transform ScaleTransform; void Update()
{
int fingersOnScreen = 0; foreach (Touch touch in Input.touches)
{
fingersOnScreen++; //Count fingers (or rather touches) on screen as you iterate through all screen touches. //You need two fingers on screen to pinch.
if (fingersOnScreen == 2)
{ //First set the initial distance between fingers so you can compare. if(touch.phase == TouchPhase.Began){ initialFingersDistance = Vector2.Distance(Input.touches[0].position, Input.touches[1].position); initialScale = ScaleTransform.localScale; } else{ float currentFingersDistance = Vector2.Distance(Input.touches[0].position, Input.touches[1].position); float scaleFactor = currentFingersDistance / initialFingersDistance; //transform.localScale = initialScale * scaleFactor; ScaleTransform.localScale = initialScale * scaleFactor; }
}
}
}
}
Script 2: I dragged this script to the gameobject I want to scale:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class onClickForScaling : MonoBehaviour {
void OnMouseDown()
{
CSharpscaling.ScaleTransform = this.transform;
}
}
I also used this code called "scaleTemp" below and put it in gameObject1 and dragged this gameobject to model 1 in the inspector.
Then I put the same code with a different "script name"/"public class" ("scaleTemp2") in gameObject2 and dragged this gameobject to model 2 in the inspector.
Scaling works this time on my Android device for both gameobjects. However if I make gameObject1 bigger, then gameObject2 becomes bigger too (in the same way), even though I only touched gameObject1. I would like to choose which object becomes bigger/smaller, with 2 fingers touch.
What can I do to make separate scaling possible?
Used script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class scaleTemp : $$anonymous$$onoBehaviour {
float initialFingersDistance;
Vector3 initialScale;
public Transform model1;
void Update()
{
int fingersOnScreen = 0;
foreach (Touch touch in Input.touches)
{
fingersOnScreen++; //Count fingers (or rather touches) on screen as you iterate through all screen touches.
//You need two fingers on screen to pinch.
if (fingersOnScreen == 2)
{
//First set the initial distance between fingers so you can compare.
if (touch.phase == TouchPhase.Began)
{
initialFingersDistance = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
initialScale = model1.transform.localScale;
}
else
{
float currentFingersDistance = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
float scaleFactor = currentFingersDistance / initialFingersDistance;
model1.transform.localScale = initialScale * scaleFactor;
}
}
}
}
}
Your answer
Follow this Question
Related Questions
2 Buttons on Android 2 Answers
Android OnTouch Event 1 Answer
global way of measuring size of game objects 1 Answer
How to change the scaling of a gameobject during runtime? 2 Answers
Unity 5.3 canvas scaling - Android 2 Answers