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.
using UnityEngine;
using System.Collections;
public class HouseDoor01Script : MonoBehaviour {
public Animation doorOpenAnimation;
public Animation doorCloseAnimation;
public bool doorOpen= false;
void Update() {
if (Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) {
if (doorOpen == false) {
OpenDoor();
}
else if (doorOpen == true) {
CloseDoor();
}
}
}
}
public void OpenDoor(){
doorOpenAnimation.Play();
doorOpen = true;
}
public void CloseDoor() {
doorCloseAnimation.Play();
doorOpen = false;
}
}
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:
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?
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.