In this tutorial, I am going show you how to scale and drag multiple objects individually in Augmented Reality with the touch screen.
Download Scripts: https://drive.google.com/open?id=0B4r-TEBjpRNaZjJYVExiQUgweEU
Please like and subscribe!
Facebook: https://www.facebook.com/AugmentedRealityGamingInfo/
Music: Still Need You.
Link: https://soundcloud.com/nocopyrightsounds/uplink-still-need-you-ncs-release.
Artists: @itsuplink & @AWR
Scripts:
#Script1: CSharpscaling
using UnityEngine;
using System.Collections;
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;
}
}
}
#Script2:onClickForScaling
using UnityEngine;
using System.Collections;
public class onClickForScaling : MonoBehaviour {
void OnMouseDown() {
CSharpscaling.ScaleTransform = this.transform;
}
}
#Script3:DragObject1
using UnityEngine;
using System.Collections;
public class DragObject1 : MonoBehaviour {
Vector3 dist;
float posX;
float posY;
void OnMouseDown(){
dist = Camera.main.WorldToScreenPoint(transform.position);
posX = Input.mousePosition.x - dist.x;
posY = Input.mousePosition.y - dist.y;
}
void OnMouseDrag(){
Vector3 curPos =
new Vector3(Input.mousePosition.x - posX,
Input.mousePosition.y - posY, dist.z);
Vector3 worldPos = Camera.main.ScreenToWorldPoint(curPos);
transform.position = worldPos;
}
}
#Script4:Rotate
using UnityEngine;
using System.Collections;
public class Rotate : MonoBehaviour {
// Update is called once per frame
void Update () {
transform.Rotate(0,20*Time.deltaTime,0);
}
}
0 Comments