Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by wilbertllama · Apr 13, 2017 at 12:40 PM · androidtouchscalescalingaugmented reality

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;
 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wilbertllama · Apr 11, 2017 at 11:21 PM 0
Share

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;
             }
         }
     }
 }

}

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges