I have a problem with the Translate
function. What I'm trying to do is to open doors by clicking on them. I succeeded in doing this with all the doors, but now I've added a new one and when I close it, it doesn't go back to its position.
I'll try to explain the problem better showing the code:
public override void OpeningDoor() {
Vector3 movement = new Vector3 (2.006f, 0.0f,1.793f);
Vector3 rotate = new Vector3 (0.0f, 108.3f, 0.0f);
transform.Translate (movement);
transform.Rotate (rotate);
toClose = true;
}
public override void ClosingDoor() {
Debug.Log ("Closing Door");
Vector3 movement = new Vector3 (-2.006f, 0.0f,-1.793f);
Vector3 rotate = new Vector3 (0.0f, -108.3f, 0.0f);
transform.Translate (movement);
transform.Rotate (rotate);
toClose = false;
}
The rotation is okay, no problem with that. Also when I open the door it goes to the right position, but when I close it, it doesn't go back to its position and it translates to a wrong one. Theoretically I just apply a certain movement on the X axis and Z axis, and when I close the door I decrement them of the same value.
I hope I've clearly explained the problem, if not please tell me.
Try changing the order of the transform.Rotate and transform.Translate lines in your ClosingDoor function.
Because the translation is relative to the orientation of the door, translating it before it's rotated will move it to the wrong place.
public override void OpeningDoor() {
Vector3 movement = new Vector3 (2.006f, 0.0f,1.793f);
Vector3 rotate = new Vector3 (0.0f, 108.3f, 0.0f);
transform.Translate (movement);
transform.Rotate (rotate);
toClose = true;
}
public override void ClosingDoor() {
Debug.Log ("Closing Door");
Vector3 movement = new Vector3 (-2.006f, 0.0f,-1.793f);
Vector3 rotate = new Vector3 (0.0f, -108.3f, 0.0f);
transform.Rotate (rotate);
transform.Translate (movement);
toClose = false;
}