- Home /
Rotate Object On Mouse Click
I need to rotate a 3D object 90 degrees when the player clicks on it. This is the current script I have attached to my 3D object. Unfortunately nothing happens when I click on the 3D object.
function OnMouseClick () {
transform.rotate(90);
}
Any help is greatly appreciated. Thank you.
Answer by kirrua645 · Mar 28, 2012 at 01:27 AM
I don't think OnMouseClick is a legitimate event. You probably want OnMouseDown.
edit Actually, you should use: http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseUpAsButton.html
It works on GameObjects which have GUIElement or Collider components.
Answer by sikha · Apr 20, 2012 at 11:48 AM
So i need same action! I've got a plane I want rotate the plane on 90 on mouse click! How to do it?
Answer by carlos3dx · Apr 20, 2012 at 01:15 PM
Try this:
function OnMouseOver(){
if(Input.GetMouseButtonDown(0)){
transform.Rotate(new Vector3(0,90,0));
}
}
It works for me.
Answer by Hybris · Apr 20, 2012 at 02:23 PM
I could be wrong but I think that OnMouseDown only applies to GUITextures, so what I suggest is a raycast:
#pragma strict
var range : 50;
var hittag : String = "plane";
function Update(){
//raycast vars
var direction = transform.TransformDirection(Vector3.forward);
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if(Physics.Raycast(ray, hit, range)){
if(hit.transform.tag == hittag){
hit.transform.rotation = Vector3(0, 90, 0);
//or you can use:
//hit.transform.Rotate untill its reached 90
}
}
}
Good luck!
-Hybris