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

Trying to rotate a single object when I tap on it, but instead it rotates every object?

Discussion in 'Scripting' started by southperry, Jul 25, 2022.

  1. southperry

    southperry

    Joined:
    Jul 22, 2022
    Posts:
    4
    Hi everyone, I'm brand new to unity and have run into an issue. The game I am working on will have a 5x5 grid of randomly oriented arrows that the player can tap on to rotate clockwise 90 degrees. I've written this script and attached it to each arrow (see screenshot). However, when I tap on say arrow 11, the console will show "Touched 11" four times, and then rotate all four arrows. Is this because I have the same script attached to each object, even though they should all be referencing the individual game object they're attached to? Any help is greatly appreciated! Thank you!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ArrowDirectionControl : MonoBehaviour
    6.  
    7. {
    8.     public GameObject arrow;
    9.     Collider2D col;
    10.     Vector2 touchPosWorld;
    11.     TouchPhase touchPhase = TouchPhase.Ended;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         float rand_num = Random.Range(1,5);
    16.         float z_rotation = 0;
    17.         switch(rand_num)
    18.         {
    19.             case 1:
    20.                 z_rotation = 0;
    21.                 break;
    22.  
    23.             case 2:
    24.                 z_rotation = 90;
    25.                 break;
    26.  
    27.             case 3:
    28.                 z_rotation = 180;
    29.                 break;
    30.          
    31.             case 4:
    32.                 z_rotation = 270;
    33.                 break;
    34.         }
    35.         arrow.transform.Rotate(0, 0, z_rotation);
    36.     }
    37.  
    38.     // Update is called once per frame
    39.     void Update()
    40.     {
    41.                    
    42.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == touchPhase)
    43.         {
    44.             touchPosWorld = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
    45.             Vector2 touchPosWorld2D = new Vector2(touchPosWorld.x, touchPosWorld.y);
    46.          
    47.             RaycastHit2D hitInformation = Physics2D.Raycast(touchPosWorld2D, Camera.main.transform.forward);
    48.             if (hitInformation.collider != null)
    49.             {
    50.                  GameObject touchedObject = hitInformation.transform.gameObject;
    51.                   Debug.Log("Touched " + touchedObject.transform.name);
    52.                  arrow.transform.Rotate(0, 0, -90);
    53.              }
    54.                    
    55.         }
    56.     }
    57. }
    Screenshot 2022-07-25 152231.png Screenshot 2022-07-25 152809.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,946
    Line 42 doesn't care who is clicking or where, so it responds.

    Start with some tutorials on either OnMouseDown() or object drag handlers.

    You're welcome to see some examples in my Proximity Buttons project, such as the scenes contained in the
    DemoWorldDragging
    folder.

    proximity_buttons is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/proximity_buttons

    https://github.com/kurtdekker/proximity_buttons

    https://gitlab.com/kurtdekker/proximity_buttons

    https://sourceforge.net/projects/proximity-buttons/
     
    southperry likes this.
  3. southperry

    southperry

    Joined:
    Jul 22, 2022
    Posts:
    4
    Thank you so much for that info!! Your comment on line 42 totally makes sense and I didn't realize that at all. I figured out a workaround that seems to do what I wanted for the time being, but I'm definitely gonna come back to this and make sure I'm understanding things fully.

    Code (CSharp):
    1. // Update is called once per frame
    2.     void Update()
    3.     {
    4.                    
    5.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == touchPhase)
    6.         {
    7.             touchPosWorld = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
    8.             Vector2 touchPosWorld2D = new Vector2(touchPosWorld.x, touchPosWorld.y);
    9.            
    10.             RaycastHit2D hitInformation = Physics2D.Raycast(touchPosWorld2D, Camera.main.transform.forward);
    11.             if (hitInformation.collider != null)
    12.             {
    13.                
    14.                  GameObject touchedObject = hitInformation.transform.gameObject;
    15.                
    16.                  if (arrow.transform.name == touchedObject.transform.name)
    17.                     {arrow.transform.Rotate(0, 0, -90);}
    18.              }
    19.                      
    20.         }
    21.     }