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

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);
}
}

}

Is one object the child of another?

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.

no sir

sorry, newbie here…please help me with my codes…

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…

Alchien, you can get object information by raycasting it, and then access ot its transform.

Check this: Unity - Scripting API: Physics.Raycast

If you can’t succeed, we will assit you :smile:

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:

using UnityEngine;
using System.Collections;

public class MouseRotate : MonoBehaviour {

    private bool mouseDown = false;
  
    // Update is called once per frame
    void Update ()
    {
        if (mouseDown)
            transform.Rotate(0,0, Time.deltaTime*110);
    }

    void OnMouseDown ()
    {
        mouseDown = true;
    }

    void OnMouseUp ()
    {
        mouseDown = false;
    }
}
1 Like

thanks a lot sir!! ^^

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??

I guess you can save a file, or use SQLLite.
Another option will be use PlayerPrefs I guess.

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.

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)

using UnityEngine;
using System.Collections;

public class Rotate : MonoBehaviour {

    private bool mouseDown = false;
   
    void Update () 
    {
        if (mouseDown)
            transform.Rotate(0,0, Time.deltaTime*110);

        mouseDown = false;
    }

    void OnMouseOver ()
    {
        if (Input.GetMouseButton(1))
        {
            mouseDown = true;
        }
    }
}