Search Unity

Animating a door

Discussion in 'Animation' started by Yourking77, Jun 15, 2016.

  1. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    I need help animating my door, the door is created and animated and imported from blender as a blend file. In teh inspector I added and animation and set the frames correctly, there is now an animation for opening door and closing doors if I expand the blend file in unity, I have written a script for the door to what I think should work correctly. Now I have no idea where to go from there and I need some advise.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class HouseDoor01Script : MonoBehaviour {
    6.  
    7.     public Animation doorOpenAnimation;
    8.     public Animation doorCloseAnimation;
    9.  
    10.     public bool doorOpen= false;
    11.  
    12.     void Update() {
    13.         if (Input.GetMouseButtonDown(0)) {
    14.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    15.             RaycastHit hit;
    16.             if (Physics.Raycast(ray, out hit)) {
    17.                 if (doorOpen == false) {
    18.                     OpenDoor();
    19.                 }
    20.                 else if (doorOpen == true) {
    21.                     CloseDoor();
    22.                 }
    23.             }
    24.         }
    25.     }
    26.  
    27.     public void OpenDoor(){
    28.         doorOpenAnimation.Play();
    29.         doorOpen = true;
    30.     }
    31.  
    32.     public void CloseDoor() {
    33.         doorCloseAnimation.Play();
    34.         doorOpen = false;
    35.     }
    36. }
     
  2. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
  3. SethMeshko

    SethMeshko

    Joined:
    Sep 20, 2014
    Posts:
    109
    It doesn't appear that you are referencing your animator. In order to play animations you need an animator connected to your object and you need to reference it in your code. Here is a unity tutorial that deals with this:
     
  4. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    I hate it when I have to watch a video to figure things out, plus everyone on youtube has an accent XD I watched it and am figuring things out a little more now, thank you. do I have to export models with animations as an fbx? that makes things a lot harder for me. Why do people do that?
     
  5. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    can you tell me what kind of animation you have on this door?
    Is it a simple rotation of the door from say 0 degree to 90 degree?
    You can do that with a script. no animator and all that crap needed.
     
  6. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    Yeah that is how I animated it, but I would rather learn to animate on simple stuff because there will be no getting around having to animate a character, might as well learn it now. that video did nto help much, when he gets to the scripting that I need to see he gets too specific and I have no idea what he is doing to activate the animation controller or the animations attached to it.
     
    Last edited: Jun 16, 2016
  7. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    I figured it out, took a lot of googling and a full day but I figured out what I was doing wrong. turns out I did not need the Animator controller at all I just needed to reference the Animator component.
     
  8. SethMeshko

    SethMeshko

    Joined:
    Sep 20, 2014
    Posts:
    109
    Good for you!

    I hate to tell you this, but in the beginning it's all about watching tutorials and sifting through tons of what seems like superfluous information. You will just have to accept that it's going to take a lot of time and that the flow of information won't be catered to your needs at the time. It may take months but eventually the information will come forward.
     
  9. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    Oh I am aware of that, but I just hate watching videos, I can deal with written tutorials and reading up on the functions and everything like that but I just hate watching videos if I really do not need to.
     
  10. derf

    derf

    Joined:
    Aug 14, 2011
    Posts:
    356
    I hate necroing an old thread but did not want to create a new one to go over something similar.


    Question, when animating a door and the door handle; is it best to put both animations in the same animation together or should both elements have different animation for them; timed by the frames between them?

    I created an animation of the door handle turning and than the door opens up.

    It is all in one animation called open_door. Complete with sound effects.

    However I am trying to determine which is the best approach for this type of animating (and perhaps to other types as well).

    My other option was to create two separate animations. One for the door and one for the door handle.

    The door handle animation would go from Key Frame 0 -12 and the door animation would go from 10-60. 0-9 the door position would not change. So when the player triggers the animation the handle would move followed close behind by the door. This way when they close the door I can reverse the speed value to a negative and the animation plays in reverse of the door "closing" but the handle doesn't move. In this way neither animation is affected by the other.

    So which is the best approach?
     
  11. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    My first thought was. Do you have have any reason for the door or the handle to animate independently. That's the only reason I can think of to have them separate.
    And then your last sentence answered that. So yes I would have 2 different animation so they can be played independently.
     
  12. keenkelel

    keenkelel

    Joined:
    Jan 15, 2022
    Posts:
    3
    Great video and tutorial....thanks a lot for this! I'm having a DoorAnimation, error, expected issue. Do you have the code where I can view and compare without going back and forth in video? Thanks so much for what you've done. I plan to look at each tutorial. Can't thank you enough, but if I get the opp to support I certainly will.
     
    Last edited: Mar 9, 2022
  13. keenkelel

    keenkelel

    Joined:
    Jan 15, 2022
    Posts:
    3

    Well, there are now only 4 errors instead of nine, some of the ( were out of place....Top level statement must precede type or namespace definition or end of file expected...? Thanks for any help.
     
  14. keenkelel

    keenkelel

    Joined:
    Jan 15, 2022
    Posts:
    3

    Well....just 2 errors now...a } was placed the wrong way...HashIDs and PlayerInventory errors now, but I figured I'd have to do the previous lesson that established these scripts......so, off to that...thanks so much for the videos and forum.