Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Rotate object by swipe rotates all objects

Discussion in 'Scripting' started by izeko2004, Apr 9, 2015.

  1. izeko2004

    izeko2004

    Joined:
    Dec 28, 2014
    Posts:
    74
    I have 2 cubes with the following script applied. The problem is when i swipe one object to rotate it it rotates both.
    Can someone tell me why?
    Code (JavaScript):
    1. var targetItem : GameObject;
    2.  
    3. /********Rotation Variables*********/
    4. var rotationRate : float = 1.0;
    5. private var wasRotating;
    6. var hit: RaycastHit;
    7. private var layerMask = (1 <<  8) | (1 << 2);
    8. function Start()
    9. {
    10.      layerMask =~ layerMask;  
    11. }
    12. function FixedUpdate()
    13. {  
    14.      if (Input.touchCount > 0)
    15.      {        //    If there are touches...
    16.              var theTouch : Touch = Input.GetTouch(0);        //    Cache Touch (0)
    17.            
    18.              var ray = Camera.main.ScreenPointToRay(theTouch.position);  
    19.                
    20.               if(Physics.Raycast(ray,hit,50,layerMask))
    21.               {  
    22.                  if(Input.touchCount == 1)
    23.                          {                          
    24.                              if (theTouch.phase == TouchPhase.Began)
    25.                               {
    26.                                   wasRotating = false;  
    27.                               }      
    28.                              
    29.                               if (theTouch.phase == TouchPhase.Moved)
    30.                               {
    31.                                  
    32.                                   targetItem.transform.Rotate(0, theTouch.deltaPosition.x * rotationRate,0,Space.World);
    33.                                   wasRotating = true;
    34.                               }      
    35.              
    36.                              }
    37.                  }
    38.            }
    39. }
     
  2. Devin-Curry

    Devin-Curry

    Joined:
    Sep 30, 2013
    Posts:
    22
    That is happening because that class that both cubes have on them is just looking to see if the first touch on screen hit something, it doesn't use the info you gather in your 'hit' variable to determine what object to call your logic on.

    I've done a tutorial on this subject (video on YouTube and text on my site). It's in C# but the logic is pretty much the same. The main thing you're missing is a line like I have on line 31 of my site:

    Code (CSharp):
    1. //if the thing that was hit implements ITouchable3D
    2. touchedObject = rayHitInfo.transform.GetComponent(typeof(ITouchable3D)) as ITouchable3D;
    But in your script since you have this script on the actual object you could do something like:
    Code (CSharp):
    1. if(hit.transform == this.transform)
    2.     //Call the rotate function
     
  3. izeko2004

    izeko2004

    Joined:
    Dec 28, 2014
    Posts:
    74
    Thanks for this. I will try it.