Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Start Animation on mouse click.

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

  1. RoyWilliams

    RoyWilliams

    Joined:
    Nov 2, 2016
    Posts:
    8
    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!!

    Thanks!!!
     
  2. neill94

    neill94

    Joined:
    Mar 3, 2013
    Posts:
    6
    Hey,

    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):
    1.  
    2.     Animator anim;
    3.  
    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 - https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

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

    AnimTut.png
     
    Anran1015 likes this.
  3. RoyWilliams

    RoyWilliams

    Joined:
    Nov 2, 2016
    Posts:
    8
    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

    Fox_GAMING

    Joined:
    May 7, 2018
    Posts:
    1
    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.