Hello, I have a problem with my script. I want to do this: <<We have two objects, if I click object1(a cube) and AFTER object2(a cylinder), object2 has to rotate>>.(Sorry for my english!) So, I wrote two scripts!
In the object1:
var object: GameObject;
var e: boolean;
function OnMouseDown(){
e=true;
object.SendMessage("vedi", e);
}
In the object2:
static var ex : boolean;
ex=false;
function vedi(e){
e==ex;
}
function OnMouseDown() {
if(ex==true){
var rot = transform.rotation;
transform.rotation = rot * Quaternion.Euler(45,90,0);
}
}
“e” is a variable that permit me to rotate object2 if it is clicked and if object1 is clicked. But the scripts don’t work good. Nothing errors for the complilator but don’t work!!!
Use raycast, something like this (in Object 1)… the “Update” code should locate somewhere else (like in a Camera or such), but it should work even if it’s inside gameobject 1:
public gameobject rotatingObject; //this should be gameobject2, set this via editor
public float rotationSpeed = 1; //try different values here
function Update() {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
// check if mouse was pressed
if (Input.GetMouseButton(0)) {
// this code actually could be somewhere else, but I just placed it here..
if (Physics.Raycast(ray, out hit, 100f ) ) {
// check if we hit Object1 (you must tag object1 with a tag "Object1" for this to work
if (hit.transform.gameObject.tag.Equals("Object1")) {
//rotating object was set to "gameobject 2" via editor
rotatingObject.Rotate(Vector3.forward*Time.deltaTime*rotationSpeed);
}
}
}
}
The OnMouseUp/Down methods do a raycast internally. Saves you checking state in Update every frame. If you were to use the code posted above I would only create the Ray when the user pressed the mouse button as well.