Search Unity

Only rotate object when user click and drag on the GameObject.

Discussion in 'Scripting' started by Ivanvan12345, Sep 11, 2015.

  1. Ivanvan12345

    Ivanvan12345

    Joined:
    Sep 12, 2014
    Posts:
    17
    Hello. I want to know if there is a way to rotate the object only when the user click and drag on the gameobject. If the user click and drag anywhere else, the gameobject must not rotate.

    Here is my rotating script in C#:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RotateShelf : MonoBehaviour {
    5.  
    6.     private Camera myCam;
    7.     private Vector3 screenPos;
    8.     private float   angleOffset;
    9.  
    10.     void Start () {
    11.         myCam=Camera.main;
    12.     }
    13.  
    14.     void Update () {
    15.         //This fires only on the frame the button is clicked
    16.         if(Input.GetMouseButtonDown(0)) {
    17.             screenPos = myCam.WorldToScreenPoint (transform.position);
    18.             Vector3 v3 = Input.mousePosition - screenPos;
    19.             angleOffset = (Mathf.Atan2(transform.right.y, transform.right.x) - Mathf.Atan2(v3.y, v3.x))  * Mathf.Rad2Deg;
    20.         }
    21.         //This fires while the button is pressed down
    22.         if(Input.GetMouseButton(0)) {
    23.             Vector3 v3 = Input.mousePosition - screenPos;
    24.             float angle = Mathf.Atan2(v3.y, v3.x) * Mathf.Rad2Deg;
    25.             transform.eulerAngles = new Vector3(0,0,angle+angleOffset);
    26.         }
    27.     }
    28. }
    The object is rotating when I click and drag anywhere in the view. Is there a way to only set it to the gameobject?

    Thank you.
     
    Last edited: Sep 11, 2015
  2. Jespertheend2

    Jespertheend2

    Joined:
    Jan 14, 2015
    Posts:
    23
    you can use the OnMouseDown() function. It gets fired once the player clicks an object.
    You can create a new bool that get's set to true in the OnMouseDown, and get's set to false once the player releases his mouse button.
     
  3. Ivanvan12345

    Ivanvan12345

    Joined:
    Sep 12, 2014
    Posts:
    17
    Thank you Jespertheend2.

    I added a bool and when the mouse is down it is = true, and when mouse up it is = false;

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RotateShelf : MonoBehaviour {
    5.    
    6.     private Camera myCam;
    7.     private Vector3 screenPos;
    8.     private float   angleOffset;
    9.     bool MouseActive = false;
    10.    
    11.     void Start () {
    12.         myCam=Camera.main;
    13.     }
    14.    
    15.     void Update () {
    16.         //This fires only on the frame the button is clicked
    17.         if(Input.GetMouseButtonDown(0)) {
    18.             if (MouseActive == true){
    19.             screenPos = myCam.WorldToScreenPoint (transform.position);
    20.             Vector3 v3 = Input.mousePosition - screenPos;
    21.             angleOffset = (Mathf.Atan2(transform.right.y, transform.right.x) - Mathf.Atan2(v3.y, v3.x))  * Mathf.Rad2Deg;
    22.             }
    23.         }
    24.         //This fires while the button is pressed down
    25.         if(Input.GetMouseButton(0)) {
    26.             if (MouseActive == true){
    27.             Vector3 v3 = Input.mousePosition - screenPos;
    28.             float angle = Mathf.Atan2(v3.y, v3.x) * Mathf.Rad2Deg;
    29.             transform.eulerAngles = new Vector3(0,0,angle+angleOffset);
    30.             }
    31.         }
    32.         }
    33.     void OnMouseDown(){
    34.         MouseActive = true;
    35.         }
    36.  
    37.     void OnMouseUp(){
    38.         MouseActive = false;
    39.     }
    40.  
    41. }
     
    AnaLuyza and YumeON like this.
  4. hahahpizza

    hahahpizza

    Joined:
    Sep 9, 2015
    Posts:
    11
    @Jespertheend2
    Is there any way to increase / decrease the speed of the rotation?
     
    Last edited: Dec 18, 2016
  5. Jespertheend2

    Jespertheend2

    Joined:
    Jan 14, 2015
    Posts:
    23
    You can multiply 'angle' from the script by a number so for example
    Code (CSharp):
    1. transform.eulerAngles=newVector3(0,0,angle*5f+angleOffset);
     
    hahahpizza likes this.
  6. Deepoo

    Deepoo

    Joined:
    Apr 15, 2021
    Posts:
    4
    @Jespertheend2 how do you make the gameObject A rotate when gameobject B is clicked and dragged up or down?
     
  7. YumeON

    YumeON

    Joined:
    Jul 16, 2020
    Posts:
    1
    Thank you Thank you Thank you!! Omg I spent so much time searching for a script that will do this! I've spent so much time, trying to combine scripts I've found that didn't work like I wanted them to work. And nooow finally I found your script! You are my hero, thank you! Your script works perfect .o.