Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Start Animation on mouse click.

Discussion in 'Animation' started by RoyWilliams, Nov 20, 2016.

  1. RoyWilliams


    Nov 2, 2016
    Hi, this seems like the easiest question to ask here but all the searching on forums and YouTube turns up either way too complicated or outdated solutions.

    I have an FBX object with embedded animation of a car that drives in a circle. I've loaded the object and attached the animation to the object and it drives exactly how it does in the 3d package. Perfect.

    But.. how do I get the animation to only start when I click on the object or (even better) a button?

    I've even created another node in the Animator between the "Entry" node and "Base Stack" (Animation file in the FBX file) node as I read that you can do that and use that node to start the animation.

    My strength is 3D, not coding so "even an idiot can follow this" steps would be great!!

  2. neill94


    Mar 3, 2013

    Here's a quick guide to setting up an animation that can be activated from code.

    1. In the Animation controller add an Idle state with no animation (Right click and set as default state).
    2. Right click and add a transition between the Idle state and your animation state
    3. In the parameters section to the right, click the + button and add a Trigger, and call it Active.
    4. Click on the transition link and un-check exit time, and add the condition Active.
    4. Add the following code to a script attached to the same object the animator controller is in the scene or link it via a public variable.

    Code (CSharp):
    2.     Animator anim;
    4.     void Start()
    5.     {
    6.         anim = gameObject.GetComponent<Animator>();
    7.     }
    8.     void Update()
    9.     {
    10.         if (Input.GetMouseButtonDown(0))
    11.         {
    12.             anim.SetTrigger("Active");
    13.         }
    14.     }
    This will trigger the animation to play when the Left Mouse button is pressed.

    If you want to activate the animation by clicking on the object, it's similar but you will need to look into Raycasting -

    Take a look at the attached screenshot of the Animator controller for help.

    jimmy14mac and Anrnzz like this.
  3. RoyWilliams


    Nov 2, 2016
    Works like a charm! Thanks a million mate. If you need any portion of 3d work done for anything you're working on gratis let me know.
  4. Fox_GAMING


    May 7, 2018
    Could you please help me? I'm in unity 3d, and I'm trying to open up fridge doors. There are 2. I want to left click to open it, and left click again to close it. I'm using C#, and my Unity 3D Version is 2018.1.0f2. I have 2 animations, too; an OPEN and a CLOSE animation.
  5. Wizzurd-Of-Uzz


    May 25, 2015
    Please keep the answers limited to answers. if you have a question please post it as a new post entirely thank you for your time.