Hi guys, I hate to ask but i don't know any coding what so ever and was wondering if i could get some help with a bit of code to opening a door, i got this code from a youtube video but i dont think the OP responds there anymore, basically i got the code working correctly and even added a line to play audio when it opens, which works fine, the door opens in the game as it should only it doesn't stop at the 90deg it is supposed to, it just keeps spinning 360deg back to it's original position then starts again, kind of like it is on loop or something, here is the code, Code (csharp): // Smothly open a door var smooth = 2.0; var DoorOpenAngle = 90.0; private var open : boolean; private var enter : boolean; private var defaultRot : Vector3; private var openRot : Vector3; function Start(){ defaultRot = transform.eulerAngles; openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z); } //Main function function Update (){ if(open){ //Open door transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth); }else{ //Close door transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth); } if(Input.GetKeyDown("f") enter){ open = !open; audio.Play();} } function OnGUI(){ if(enter){ GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "Press 'F' to open the door"); } } //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; } } //@youtube.com/user/maksimum654321 Now like i said it works as in i can walk up to it and press F and the door opens but just keeps spinning, not all crazy like just spins in a 360 deg motion instead of opening to 90deg and stopping. Anyhelp would be appreciated if someone knows what may be causing it to do this.
ok nvm i figured it out, my parent objects position was not set to 0 once i set it to zero it worked fine.
I don't see where the var open is initialized. Maybe use var open:boolean = false; I would do the same for enter. Also, check you console for any warnings, etc.
ok i got the door functioning correctly would anyone know how i would make it open the other way, when i walk up to the door it opens towards me at 90deg but i need it to open at -90 the away from, i have tried changing this in the script to -90 but my door just has a heart attack and starts flying all over the place.
If 90 is toward you then away from you would be 270. However, you would need the door to rotate in the opposite direction. Maybe try changing the smooth value to -2.0. I don't know if that will work or not, or try -90 and -2.0.
Maybe, try to flip the hinge (object) rotation.Y 180 degrees. I had an issue similar to this, and the rotation.y for my model was already 180 degrees, so I just inverted that by simply making it -180 degrees. Hope this helps.
How exactly did you change the objects position to 0? I am dealing with the exact same issue ,and although i changed the object position to 0 it still kept spinning. Also moved it out of scene. It was working in an old scene I was messing with, but then I rescaled the level, and lost proper function on that object. .....Solved....... Edit: I must have just lost some integrity on my model when I made a prefab from it. I decided to delete my hinge (object) and Trigger Collider,, and remake them. I attached my door script and all was well.