- Home /
The question is answered, right answer was accepted
Simple Script - and no result
Can someone tell me what I have done wrong? This is a simple script that should open and close a double door after pushing a button on a wall.
When I test it I get "b" in the debug but not "a" - so the doors close but don't open again (also note the doors were closed to start with)
var door_left : Transform;
var door_right : Transform;
var door_left_startX : float; var door_right_startX : float;
var countA : float = 0; var door : boolean = true;
function Update () { var hit : RaycastHit;
//find forward then test for x meters in front of position if (Physics.Raycast (transform.position, transform.forward, hit, 2)) { if(hit.collider.gameObject.tag=="Player" && Input.GetAxis ("e_key") ) {
if (door == true && countA == 0)
{
countA = -1;
door = false;
print ( "a");
}
if (door == false && countA == 0)
{
countA = 1;
door = true;
print ( "b");
}
}
}
if (countA > 0) { countA = countA - 0.01; }
if (countA < 0) { countA = countA + 0.01; }
door_left.transform.position = Vector3(door_left_startX+(countA*-1), door_left.transform.position.y, door_left.transform.position.z); door_right.transform.position = Vector3(door_right_startX+countA, door_right.transform.position.y, door_right.transform.position.z);
}
Answer by Myth · Jan 29, 2011 at 01:49 AM
Finally it works! - wouldn't have figured it out if you hadn't told me about the Boolean thingy though.
Thanks for your help!
Here is the final code:
var door_left : Transform;
var door_right : Transform;
var door_left_startX : float; var door_right_startX : float;
var countA : float = 0; var opening : int = 0;
function Update () { var hit : RaycastHit;
//find forward then test for x meters in front of position if (Physics.Raycast (transform.position, transform.forward, hit, 2)) { if(hit.collider.gameObject.tag=="Player" && Input.GetAxis ("e_key") ) { if (countA <= 0) { opening = -1; } if (countA >= 1) { opening = 1; } } }
if (opening == 1 && countA > 0) { countA = countA - 0.01; }
if (opening == -1 && countA < 1) { countA = countA + 0.01; }
door_left.transform.position = Vector3(door_left_startX+(countA*-1), door_left.transform.position.y, door_left.transform.position.z); door_right.transform.position = Vector3(door_right_startX+countA, door_right.transform.position.y, door_right.transform.position.z);
}
Hi, you are right, I made a mistake with the parameters on the door position stuff, now I made a correction and the function work fine, I got two doors working with just one value that open when the character it's close enough and close the doors whe the character pass through the doors.
Answer by 3DMagicVR · Jan 28, 2011 at 06:16 AM
Hi, try to use this:
function Update() { var hit : RaycastHit; if (Physics.Raycast (transform.position, transform.forward, hit, 2)) { if(hit.collider.gameObject.tag == "Player" && /* The button action is activated */) { if (door == true) { // Opend the doors countA += 0.01; } } }
if (countA == 1) { // The doors are totally open
door = false;
}
if (countA > 0 && door == false) { // Close the doors
countA -= 0.01;
}
door_Left.localPosition = Vector3(-countA, 0, 0);
door_Right.localPosition = Vector3(countA, 0, 0);
}
This should work, when work with two objects (one in front another) just type value and -value to set positive and negative motion, the recommendation it's to use always positive number so this can be made negative using the "-" before the value, if you use the boolean state in a "if" statement with the same boolean as a condition this will interrupt if condition and the loop will be interrupted.
Hopes this help.
Sorry, I have been unable to get this to produce the desired result. With your code the doors close and end up on top of each other and then as with my code I am unable to "Open" the doors again.
Thanks for attempting to help