Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Need help please on a script

Discussion in 'Scripting' started by Adragen, Oct 9, 2013.

  1. Adragen

    Adragen

    Joined:
    May 4, 2013
    Posts:
    25
    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):
    1. // Smothly open a door
    2. var smooth = 2.0;
    3. var DoorOpenAngle = 90.0;
    4. private var open : boolean;
    5. private var enter : boolean;
    6.  
    7. private var defaultRot : Vector3;
    8. private var openRot : Vector3;
    9.  
    10. function Start(){
    11. defaultRot = transform.eulerAngles;
    12. openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z);
    13. }
    14.  
    15. //Main function
    16. function Update (){
    17. if(open){
    18. //Open door
    19. transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
    20. }else{
    21. //Close door
    22. transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
    23. }
    24.  
    25. if(Input.GetKeyDown("f")  enter){
    26. open = !open;
    27. audio.Play();}
    28. }
    29.  
    30. function OnGUI(){
    31. if(enter){
    32. GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "Press 'F' to open the door");
    33. }
    34. }
    35.  
    36. //Activate the Main function when player is near the door
    37. function OnTriggerEnter (other : Collider){
    38. if (other.gameObject.tag == "Player") {
    39. enter = true;
    40. }
    41. }
    42.  
    43. //Deactivate the Main function when player is go away from door
    44. function OnTriggerExit (other : Collider){
    45. if (other.gameObject.tag == "Player") {
    46. enter = false;
    47. }
    48. }
    49. //@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.
     
    Last edited: Oct 9, 2013
  2. Adragen

    Adragen

    Joined:
    May 4, 2013
    Posts:
    25
    ok nvm i figured it out, my parent objects position was not set to 0 once i set it to zero it worked fine.
     
  3. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,685
  4. Adragen

    Adragen

    Joined:
    May 4, 2013
    Posts:
    25
  5. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    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.
     
    Last edited: Oct 9, 2013
  6. Adragen

    Adragen

    Joined:
    May 4, 2013
    Posts:
    25
    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.
     
  7. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    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.
     
  8. a.Scarselli

    a.Scarselli

    Joined:
    Oct 25, 2013
    Posts:
    16
    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.
     
  9. a.Scarselli

    a.Scarselli

    Joined:
    Oct 25, 2013
    Posts:
    16
    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.
     
    Last edited: Oct 26, 2013
unityunity