Search Unity

Simple button that opens animation of a door.

Discussion in 'Animation' started by meierdesigns, May 29, 2017.

  1. meierdesigns

    meierdesigns

    Joined:
    Aug 3, 2016
    Posts:
    22
    Hey guys,
    I was wondering if you could help me with this:

    I want to start an animation of an object with an object in a first person environment.
    In my case that is an animation for a door that is controlled by a button next to it.
    I don't want to code the animation of the object, so that's why I want to use the animator and animations.

    First of all, I'm not that used to programming, please be patient with me.

    I have several questions:

    Do I need to raycast something in order to use the OnMouseDown function?

    How do I run animations with a different GameObject on mouse down? - I tried it already:
    Look here...:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DoorScript : MonoBehaviour {
    6.  
    7.     public GameObject doorOb;
    8.     private Animator _animatior;
    9.  
    10.    
    11.     // Use this for initialization
    12.     void Start () {
    13.     }
    14.  
    15.     void OnTriggerEnter(Collider other)
    16.     {
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update () {
    21.     }
    22.  
    23.     private void OnMouseDown()
    24.     {
    25.        //trying to play an animation here...
    26.         doorOb.GetComponent<Animation>().Play();
    27.     }
    28. }
    29.  
    Is <Animation> to be changed or do I need to insert the animation clip behind Play("OpenDoor") ?

    I really appreciate your help. Thank you so much :) I will model anything or create a substance shader to return your favor.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    the doorOb must have a collider for OnMouseDown to work.
    Your camera must have a physics Raycaster on it, I believe, also.
    If you try both of those, and it's not working, add an eventsystem to your game and try again.

    As for needing the animation clip, I think you don't if there's only 1 (that's the default). You do if there's more than 1.
    Then, you have an 'Animator' variable in your script that isn't used ;)
     
    meierdesigns likes this.
  3. meierdesigns

    meierdesigns

    Joined:
    Aug 3, 2016
    Posts:
    22
    Yeah I will try the combination of these two methods first.

    I also want to just have a set of animations, just to put on any GameObjects.

    Thanks for the quick help :)
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sure, no problem. :) Hopefully once you get one working, it'll be easier to add more.. of course.. heh.
     
  5. meierdesigns

    meierdesigns

    Joined:
    Aug 3, 2016
    Posts:
    22
    Error012.PNG

    Okay. I added a raycaster to the Camera. How do I assign an animation to the door or what do I need to add to the animator?
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, so : Animation and Animator are 2 different things. You could use either. I often see people recommend using the Animator, but some people swear for simple things that Animation is easier.
    Anyways, for the Animator, you set a parameter which transitions your animation (you have to set those up in the animator). For the animation, you can just use 'Play' as your first code was doing.
    Without getting too bogged down in the above part I said, basically you should pick one and go with that and then it should work :)

    Okay, and what you'd need to add to the animator: I think you add the animation in that window in your screenshot, maybe that's already there that thing. Then, instead of doing: "Animation.Play()." You do something like: GetComponent<Animator>().setBool("openDoor", true); to open it (false to close it). Where, 'openDoor' is a parameter you add in that empty list on the left of your screenshot :)
     
    meierdesigns likes this.
  7. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    1,383
    Hey @meierdesigns

    I created this script, it will allow you to easily create doors in your scene for free! The code is free, easy to read and nicely commented out so you should be able to learn a thing or two from it when it comes to doors!

    Good luck,

    Alex