what's wrong with this JS code (very short)??

so my door will swing open and shut very smoothly with the press of F. however sometimes it does not and will use the door open animation when the door is closed. i cannot figure out why this is, but my best guess is it has to do with the speed at which F is being used. also if I press F once to use the animation, and then press F again during the animation, I think it uses the close door animation next (without actually seeing it happen on screen because the first animation is still going) and that means the next F press is going to reset to the open animation

i’ll make a video if you don’t see what i mean because I can’t really explain it.

var doorhinge : Transform; // This is a reference to the door GameObject that has your animation
private var HasTriggerBeenUsed : boolean = false; // This is to make sure that the button is not repeatedly pressed.
private var setTrigger : boolean = false;
function OnTriggerStay() {
if (Input.GetKeyDown(“f”) !HasTriggerBeenUsed) {
doorhinge.animation.Play(“dooropen”);
setTrigger = true;
}
else if (Input.GetKeyDown(“f”) HasTriggerBeenUsed) {
doorhinge.animation.Play(“doorclose”);
setTrigger = true;
}
if (setTrigger) { HasTriggerBeenUsed = !HasTriggerBeenUsed; }
}

you can switch it to GetKeyUp so it only fires once each press.

thanks, i tried this and it makes it a little smoother, but still this isnt the problem, the problem is the open animation plays when the close animation is suppose to. but its inconsistent, it doesn’t always happen.

ill make a video

im very new to unity and game development in general

Have you tried stopping the animation before playing the new one?

i think problem is the new animation is starting before the 1st one finishes so the script thinks the next F key is suppose to be an open animation instead of a close.

the animation doesn’t loop or anything, however since they are attached to the same gameobject using the same keybind (the hinge), perhaps i would need to set it up so that the first animation must complete before F triggers the next one??

I don’t know how I would go about doing that though

perhaps it has something to do with the elements not the code? currently i just have element 0 = door open and element 1 = door close

here’s a video, you will see what i mean about the inconsistency

http://www.twitch.tv/goretooth_bloodhound/b/341086278

sorry about the ads, they are annoying i know

try this:

function OnTriggerStay() {
if (Input.GetKeyUp("f")) 
	{

		doorhinge.animation.Play("dooropen");
		
		if (doorhinge.animation["dooropen"].speed == 1)
		{
			doorhinge.animation["dooropen"].speed = -1;
		}
		else
		{
			doorhinge.animation["dooropen"].speed = 1;
		}
		
    }
}

now closing the door doesn’t work. it just resets to the closed position after pressing F

full code now looks like

var doorhinge : Transform;
private var HasTriggerBeenUsed : boolean = false; pressed.
private var setTrigger : boolean = false;
function OnTriggerStay() {
if (Input.GetKeyUp(“f”))
{

doorhinge.animation.Play(“dooropen”);

if (doorhinge.animation[“dooropen”].speed == 1)
{
doorhinge.animation[“dooropen”].speed = -1;
}
else
{
doorhinge.animation[“dooropen”].speed = 1;
}

}
else if (Input.GetKeyUp(“f”) HasTriggerBeenUsed) {
doorhinge.animation.Play(“doorclose”);
setTrigger = true;
}
if (setTrigger) { HasTriggerBeenUsed = !HasTriggerBeenUsed; }
}

You shouldn’t need the close animation, the code just plays the open one backwards when it’s pressed a second time.

var doorhinge : Transform;
private var HasTriggerBeenUsed : boolean = false; pressed.
private var setTrigger : boolean = false;
function OnTriggerStay() {
if (Input.GetKeyUp(“f”))
{

doorhinge.animation.Play(“dooropen”);

if (doorhinge.animation[“dooropen”].speed == 1)
{
doorhinge.animation[“dooropen”].speed = -1;
}
else
{
doorhinge.animation[“dooropen”].speed = 1;
}

}

}

i tried that exact code before trying it with the close animation in the code again and ive seen it do the rewind but usually it just finishes the animation and then resets to close position after F

would i have to build the close animation into the open so that the rewind will work? currently the open animation just rotates about 90*, but it doesnt rotate back, that is the end of the animation

found this here

going to give it a try

//Instruction:
//Make an empty game object and call it “Door”
//Rename your 3D door model to “Body”
//Parent a “Body” object to “Door”
//Make sure thet a “Door” object is in left down corner of “Body” object. The place where a Door Hinge need be
//Add a box collider to “Door” object and make it much bigger then the “Body” model, mark it trigger
//Assign this script to a “Door” game object that have box collider with trigger enabled
//Press “f” to open the door and “g” to close the door
//Make sure the main character is tagged “player”

// Smothly open a door
var smooth = 2.0;
var DoorOpenAngle = 90.0;
var DoorCloseAngle = 0.0;
var open : boolean;
var enter : boolean;

//Main function
function Update (){

if(open == true){
var target = Quaternion.Euler (0, DoorOpenAngle, 0);
// Dampen towards the target rotation
transform.localRotation = Quaternion.Slerp(transform.localRotation, target,
Time.deltaTime * smooth);
}

if(open == false){
var target1 = Quaternion.Euler (0, DoorCloseAngle, 0);
// Dampen towards the target rotation
transform.localRotation = Quaternion.Slerp(transform.localRotation, target1,
Time.deltaTime * smooth);
}

if(enter == true){
if(Input.GetKeyDown(“f”)){
open = !open;
}
}
}

//Activate the Main function when player is near the door
function OnTriggerEnter (other : Collider){

if (other.gameObject.tag == “Player”) {
(enter) = true;
}
}

//Deactivate the Main function when player is go away from door
function OnTriggerExit (other : Collider){

if (other.gameObject.tag == “Player”) {
(enter) = false;
}
}

that last script i found on youtube works flawlessly and the door animation is very realistic

i appreciate all the help though :slight_smile:

try this script out its quite nice

Can we please use code tags? It makes it much easier to read and follow code in these forums.
You don’t need to go advanced reply. Just type the tags in directly: “[ code ]” and “[ /code ]” (without spaces).

For example, typing this in the quick reply:

will yield:

/*Your code goes here and will automatically be colored*/
var foo :int;
function exampleFunc () {
...
}