Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Touch in Layers and Rotate object

Discussion in 'Scripting' started by Flumobil, Aug 7, 2014.

  1. Flumobil

    Flumobil

    Joined:
    Jun 6, 2014
    Posts:
    8
    Hello, I'm learning Unity and I am trying to manipulate the object. This object is a sphere, inside which there are layers (mantle, crust ..), as you can see in the image. How do I get when touching the mantle layer, for example, and display an information?

    Note: I am using Vuforia.

    Sorry for my english ... It's not the best. Thank you!

    print.jpg
     
  2. FlaSh-G

    FlaSh-G

    Joined:
    Apr 21, 2010
    Posts:
    212
    I guess you want all the layers to have a mesh collider and then add a Script to each one which reacts to touching that collider.
    In terms of design however, I think that the moddle layer would be hard to touch.
    I would add some Buttons on the side which can be touched instead and then add some lines to graphically connect them with the layers.
     
  3. Flumobil

    Flumobil

    Joined:
    Jun 6, 2014
    Posts:
    8
    Thanks for the reply!
    Got it ... It really would be difficult to play, but I wanted to test. Do you have any tutorial that can help me develop this script?
     
  4. FlaSh-G

    FlaSh-G

    Joined:
    Apr 21, 2010
    Posts:
    212
  5. Flumobil

    Flumobil

    Joined:
    Jun 6, 2014
    Posts:
    8
    Thank you! In case you want to make this interaction on mobile, how about it?
     
  6. FlaSh-G

    FlaSh-G

    Joined:
    Apr 21, 2010
    Posts:
    212
    I had in mind that you wanted touch - Unity fires this event when touching, too. Since version... 4.something, I think.
     
  7. Flumobil

    Flumobil

    Joined:
    Jun 6, 2014
    Posts:
    8
    Thanks FlaSh.G!

    Searching, I found an interesting code. I adapted to my object. But this code was developed to meet only one object ball format. How do I adapt to another format? e.g., a neuron.



    Code (CSharp):
    1. ///////////////////////////////////////////////////////////
    2. //
    3. //   Author  : Alexander Orozco
    4. //   Email   : alex@rozgo.com
    5. //   License : Keep this notice around. Otherwise, enjoy!
    6. //
    7. ///////////////////////////////////////////////////////////
    8.  
    9. using UnityEngine;
    10. using System.Collections;
    11.  
    12. public class Gestures : MonoBehaviour {
    13.    
    14.     // adjust accordingly in the inspector
    15.     public float zoomNearLimit = 5;
    16.     public float zoomFarLimit = 12;
    17.     public float zoomScreenToWorldRatio = 3.0f;
    18.     public float orbitScreenToWorldRatio = 1.0f;
    19.     public float twistScreenToWorldRatio = 5.0f;
    20.    
    21.     // don't change these
    22.     Vector3 orbitSpeed = Vector3.zero;
    23.     float twistSpeed = 0;
    24.     float distWeight;
    25.     float zoomDistance;
    26.     float zoomSpeed = 0;
    27.     float lastf0f1Dist;
    28.    
    29.     void Update () {
    30.  
    31.         // one finger gestures
    32.         if (Input.touchCount == 1) {
    33.            
    34.             // finger data
    35.             Touch f0 = Input.GetTouch(0);
    36.            
    37.             // finger delta
    38.             Vector3 f0Delta = new Vector3(f0.deltaPosition.x, -f0.deltaPosition.y, 0);
    39.            
    40.             // if finger moving
    41.             if (f0.phase == TouchPhase.Moved) {
    42.                
    43.                 // compute orbit speed
    44.                 orbitSpeed += (f0Delta + f0Delta * distWeight) * orbitScreenToWorldRatio * Time.deltaTime;
    45.             }
    46.         }
    47.        
    48.         // two fingers gestures
    49.         else if (Input.touchCount == 2) {
    50.            
    51.             // fingers data
    52.             Touch f0 = Input.GetTouch(0);
    53.             Touch f1 = Input.GetTouch(1);
    54.            
    55.             // fingers positions
    56.             Vector3 f0Pos = new Vector3(f0.position.x, f0.position.y, 0);
    57.             Vector3 f1Pos = new Vector3(f1.position.x, f1.position.y, 0);
    58.            
    59.             // fingers movements
    60.             Vector3 f0Delta = new Vector3(f0.deltaPosition.x, f0.deltaPosition.y, 0);
    61.             Vector3 f1Delta = new Vector3(f1.deltaPosition.x, f1.deltaPosition.y, 0);
    62.            
    63.             // fingers distance
    64.             float f0f1Dist = Vector3.Distance(f0.position, f1.position);
    65.            
    66.             // if both fingers moving
    67.             if (f0.phase == TouchPhase.Moved && f1.phase == TouchPhase.Moved) {
    68.                
    69.                 // fingers moving direction
    70.                 Vector3 f0Dir = f0Delta.normalized;
    71.                 Vector3 f1Dir = f1Delta.normalized;
    72.                
    73.                 // dot product of directions
    74.                 float dot = Vector3.Dot(f0Dir, f1Dir);
    75.                
    76.                 // if fingers moving in opposite directions
    77.                 if (dot < -0.7f) {
    78.                    
    79.                     float pinchDelta = f0f1Dist - lastf0f1Dist;
    80.                    
    81.                     // if fingers move more than a threshold
    82.                     if (Mathf.Abs(pinchDelta) > 2) {
    83.                        
    84.                         // if pinch out, zoom in
    85.                         if (f0f1Dist > lastf0f1Dist && zoomDistance > zoomNearLimit) {
    86.                             zoomSpeed += (pinchDelta + pinchDelta * distWeight) * Time.deltaTime * zoomScreenToWorldRatio;
    87.                         }
    88.                        
    89.                         // if pinch in, zoom out
    90.                         else if (f0f1Dist < lastf0f1Dist && zoomDistance < zoomFarLimit) {
    91.                             zoomSpeed += (pinchDelta + pinchDelta * distWeight) * Time.deltaTime * zoomScreenToWorldRatio;
    92.                         }
    93.                     }
    94.                    
    95.                     // detect twist
    96.                     if (f0Delta.magnitude > 2 && f1Delta.magnitude > 2) {
    97.                        
    98.                         // homemade algorithm works, but needs code review
    99.                         Vector3 fingersDir = (f1Pos - f0Pos).normalized;
    100.                         Vector3 twistNormal = Vector3.Cross(fingersDir, Vector3.forward);
    101.                         Vector3 twistAxis = Vector3.Cross(fingersDir, twistNormal);
    102.                         float averageDelta = (f0Delta.magnitude + f1Delta.magnitude) / 2;
    103.                         if (Vector3.Dot(f0Dir, twistNormal) > 0.7f) {
    104.                             twistSpeed =  twistAxis.z * averageDelta * Time.deltaTime * twistScreenToWorldRatio;
    105.                         }
    106.                         else if (Vector3.Dot(f0Dir, twistNormal) < -0.7f) {
    107.                             twistSpeed = -twistAxis.z * averageDelta * Time.deltaTime * twistScreenToWorldRatio;
    108.                         }
    109.                     }
    110.                 }
    111.             }
    112.            
    113.             // record last distance, for delta distances
    114.             lastf0f1Dist = f0f1Dist;
    115.            
    116.             // decelerate zoom speed
    117.             zoomSpeed = zoomSpeed * (1 - Time.deltaTime * 10);
    118.         }
    119.        
    120.         // no touching, or too many touches (we don't care about)
    121.         else {
    122.            
    123.             // bounce to zoom limits
    124.             if (zoomDistance < zoomNearLimit) {
    125.                 zoomSpeed += (zoomDistance - zoomNearLimit) * zoomScreenToWorldRatio;
    126.             }
    127.             else if (zoomDistance > zoomFarLimit) {
    128.                 zoomSpeed += (zoomDistance - zoomFarLimit) * zoomScreenToWorldRatio;
    129.             }
    130.            
    131.             // or decelerate
    132.             else {
    133.                 zoomSpeed = zoomSpeed * (1 - Time.deltaTime * 10);
    134.             }
    135.         }
    136.        
    137.         // decelerate orbit speed
    138.         orbitSpeed = orbitSpeed * (1 - Time.deltaTime * 5);
    139.  
    140.         // decelerate twist speed
    141.         twistSpeed = twistSpeed * (1 - Time.deltaTime * 5);
    142.  
    143.         // apply zoom
    144.         transform.position += transform.forward * zoomSpeed * Time.deltaTime;
    145.         zoomDistance = transform.position.magnitude;
    146.        
    147.         // apply orbit and twist
    148.         transform.position = Vector3.zero;
    149.         transform.localRotation *= Quaternion.Euler(orbitSpeed.y, orbitSpeed.x, twistSpeed);
    150.         transform.position = -transform.forward * zoomDistance;
    151.        
    152.         // compensate for distance (ej. orbit slower when zoomed in; faster when out)
    153.         distWeight = (zoomDistance - zoomNearLimit) / (zoomFarLimit - zoomNearLimit);
    154.         distWeight = Mathf.Clamp01(distWeight);
    155.     }
    156. }
    157.  
     
  8. Flumobil

    Flumobil

    Joined:
    Jun 6, 2014
    Posts:
    8
    I changed the title, did not agree with the initial proposal....
     
  9. FlaSh-G

    FlaSh-G

    Joined:
    Apr 21, 2010
    Posts:
    212
    You shouldn't rely on some scripts you found on the web. Especially if they do not exactly do what you want. Analysing this script and changing it to satisfy certain needs often is more work than to write something new.