- Home /
 
Rotate clicked object
Im wanting to make a script that when I click an object it rotates it 90 degress. The trick is I have a stack of blocks and I want to be able to click on any of them and rotate only the clicked one. So if I have a stack of blocks 4 high I can click on the bottom one and rotate it 90 degress or click any of them in the stack and rotate the clicked on 90 degress. I know to do the rotation I need this:
 transform.Rotate(new Vector3(0,90,0));
 
              Answer by InfiniBuzz · Jul 01, 2013 at 03:50 PM
create a new script and add this code:
 void Update(){
     if(Input.GetMouseButtonDown(0){
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycstHit hit;
        if(Physics.Raycast(ray, out hit)){
           if(hit.collider.tag == "MyBlock"{
              // Do your rotation here
           }
        }
     }
 }
 
               Attach this script to each of your blocks and make sure they have a collider attached and are tagged properly (MyBlock).
Answer by Travis-Bulford · Jul 01, 2013 at 03:51 PM
Are you looking for the following?
 public void OnMouseDown()
 {
     transform.Rotate(new Vector3(0f,90f,0f));
     // Or do you need this instead?
     // transform.Rotate(0f, 90f, 0f, Space.World);
 }
 
              Answer by asduffo · Jul 01, 2013 at 04:16 PM
create a script with the following code and attach it to the object you want to move:
 public void OnMouseDown()
 {
      transform.Rotate(new Vector3(0,90,0));
 }
 
               if using javascript:
 function OnMouseDown()
 {
     transform.Rotate(new Vector3(0,90,0));
 }
 
              Your answer