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. Dismiss Notice

please guys help me....!!! Rotating an Object~~

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

  1. Alchien

    Alchien

    Joined:
    Aug 7, 2014
    Posts:
    11
    I just want to rotate an object by clicking the object..but when I used two objects and have the same script, they both rotate... what I want is to rotate one object when I click, and then when I click the other object it also rotate..please help me guys~

    //this is the codes//

    using UnityEngine;
    using System.Collections;

    public class clickToRotate : MonoBehaviour {

    void Update(){
    if(Input.GetMouseButtonDown (1)){
    transform.Rotate(0,0, Time.deltaTime*110);
    }
    }

    }
     
  2. Deleted User

    Deleted User

    Guest

    Is one object the child of another?
     
  3. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    Please use Code Tags when posting code on the forum.

    Your script doesn't do what you think it does. You're simply checking that a click has happened, and not what/where the click is in relevance to anything. All objects with that script on will rotate whenever you click.
     
  4. Alchien

    Alchien

    Joined:
    Aug 7, 2014
    Posts:
    11
    no sir
     
  5. Alchien

    Alchien

    Joined:
    Aug 7, 2014
    Posts:
    11
    sorry, newbie here..please help me with my codes..
     
  6. Alchien

    Alchien

    Joined:
    Aug 7, 2014
    Posts:
    11
    no sir.. I'm creating an app for interior design that have edit tools... like can select and dropNdrag furnitures, can delete,can rotate and scale it. please help me with this.. I'm only student this is for our project...
     
  7. Kokumo

    Kokumo

    Joined:
    Jul 23, 2010
    Posts:
    416
  8. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    Using OnMouseDown is also another good option. It all depends on the behaviour and complexity you're trying to achieve.

    Something as simple this might do:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouseRotate : MonoBehaviour {
    5.  
    6.     private bool mouseDown = false;
    7.  
    8.     // Update is called once per frame
    9.     void Update ()
    10.     {
    11.         if (mouseDown)
    12.             transform.Rotate(0,0, Time.deltaTime*110);
    13.     }
    14.  
    15.     void OnMouseDown ()
    16.     {
    17.         mouseDown = true;
    18.     }
    19.  
    20.     void OnMouseUp ()
    21.     {
    22.         mouseDown = false;
    23.     }
    24. }
    25.  
     
    Kokumo likes this.
  9. Alchien

    Alchien

    Joined:
    Aug 7, 2014
    Posts:
    11

    thanks a lot sir!! ^^
     
  10. Alchien

    Alchien

    Joined:
    Aug 7, 2014
    Posts:
    11
    sir...how can I save my scene? after to edit the position,rotation of the objects I want also to save it..how to do that??
     
  11. Kokumo

    Kokumo

    Joined:
    Jul 23, 2010
    Posts:
    416
    I guess you can save a file, or use SQLLite.
    Another option will be use PlayerPrefs I guess.
     
  12. Alchien

    Alchien

    Joined:
    Aug 7, 2014
    Posts:
    11
    this codes is exactly what i want but i want to use the right click of mouse to rotate the objects..please help me sir.
     
  13. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    Using right click gets a little trickier, but this script is the simplest way I could find. Note that the behaviour is changed slightly and the objects will stop rotating as soon as the mouse moves off them (in the old script they only stopped when the button came up)

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Rotate : MonoBehaviour {
    6.  
    7.     private bool mouseDown = false;
    8.    
    9.     void Update ()
    10.     {
    11.         if (mouseDown)
    12.             transform.Rotate(0,0, Time.deltaTime*110);
    13.  
    14.         mouseDown = false;
    15.     }
    16.  
    17.     void OnMouseOver ()
    18.     {
    19.         if (Input.GetMouseButton(1))
    20.         {
    21.             mouseDown = true;
    22.         }
    23.     }
    24. }
    25.