Search Unity

Start Animation on mouse click.

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

  1. RoyWilliams

    RoyWilliams

    Joined:
    Nov 2, 2016
    Posts:
    11
    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
     
  3. RoyWilliams

    RoyWilliams

    Joined:
    Nov 2, 2016
    Posts:
    11
    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.
     
    aeternum-vale likes this.
  4. Fox_GAMING

    Fox_GAMING

    Joined:
    May 7, 2018
    Posts:
    12
    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.
     
    Ahkshara likes this.
  5. Wizzurd-Of-Uzz

    Wizzurd-Of-Uzz

    Joined:
    May 25, 2015
    Posts:
    3
    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.
     
  6. wyldelionel

    wyldelionel

    Joined:
    Jan 10, 2020
    Posts:
    1
    hey this script isnt working..... any thing i missed?
    i followed instructions script invalid
     
  7. SOICGeek

    SOICGeek

    Joined:
    Jun 25, 2019
    Posts:
    78
    @wyldelionel - Make sure you're using a fresh C# script. Put everything inside the curly braces of the class, and replace the existing Start() and Update() methods. Other than that, we'd need to see your script.
     
  8. DawPiot14

    DawPiot14

    Joined:
    Jun 10, 2020
    Posts:
    2
    Hi,

    I followed the steps and copied the code correctly but I get this error:
    Assets\On_Click.cs(4,10): error CS0116: A namespace cannot directly contain members such as fields or methods

    This is my code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    Animator anim;

    public class On_Click : MonoBehaviour
    {
    // Start is called before the first frame update
    void Start()
    {
    anim = gameObject.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
    if (Input.GetMouseButtonDown(0))
    {
    anim.SetTrigger("Active");
    }
    }
    }
     
  9. Wheatyjango

    Wheatyjango

    Joined:
    Mar 28, 2019
    Posts:
    1
    Hey @DawPiot14,

    Everything that you code other than 'using UnityEngine...' etc must be inside the class itself. Your issue is that your anim variable is outside of the class, which will most certainly cause some issues. Just move the variable inside of the MonoBehaviour class and you'll be good to go! ;)
     
  10. EmergenctyLlama

    EmergenctyLlama

    Joined:
    Nov 24, 2020
    Posts:
    1
    This would have been a great way for my animation to play when I click but sadly this does not work in unity 5 :mad:, please fix this soon, I know this may be a problem for some people:D
     
  11. nikitavanvliet2002

    nikitavanvliet2002

    Joined:
    Nov 30, 2020
    Posts:
    2
     
  12. nikitavanvliet2002

    nikitavanvliet2002

    Joined:
    Nov 30, 2020
    Posts:
    2
    Because you have (Animator anim;) up it need under public class
     
  13. Zaxify

    Zaxify

    Joined:
    Aug 14, 2019
    Posts:
    2
    The code snippet you attached does not work. This is what the error said:

    Assets\playanim.cs(8,13): error CS0116: A namespace cannot directly contain numbers such as fields or methods
    Assets\playanim.cs(8,20): error CS0246: The type or namespace name "Animator" could not be found (are you missing a using directive or an assembly refrence?)

    Please follow up on this, I really need this. Thanks!
     
  14. darkshade015

    darkshade015

    Joined:
    Sep 24, 2020
    Posts:
    3
    hey it says "in state "NEW STATE" uses parameter" wich does not exist in controller " so idk what is happeing
     
  15. herzher

    herzher

    Joined:
    Jul 25, 2018
    Posts:
    2
    Hi,
    Hi, I Used this script and it works great. But can someone please help with how to introduce raycasting into this script so that the chest will open only when clicked on?
    Thanks a lot!
     
  16. XxZanderxX

    XxZanderxX

    Joined:
    Jun 6, 2021
    Posts:
    1
    I'm new to coding and i needed help so i went here. I copied everything that i saw on the forum and it just won't work! I keep getting this error saying "error Cs1003: syntax error, ',' expected." I tried putting in the commas but it just wont work. Need help on this.
     
  17. Ollster88

    Ollster88

    Joined:
    Mar 4, 2021
    Posts:
    1
  18. MaisamBinKhaqan

    MaisamBinKhaqan

    Joined:
    Dec 11, 2020
    Posts:
    1
    Hi all, I am trying to play door opening animation by clicking on a door, there are basically 4 doors in my scene, but when I play the scene all 4 doors animate simultaneously without clicking, for clicking functionality I even tried attaching a button to each door but it didn't work. Any help would be appreciated. Thanks!

    I am attaching a code for door animation too:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DoorRotation : MonoBehaviour
    6. {
    7.     public GameObject doorRotation;
    8.    
    9.     void Update()
    10.     {
    11.        if(Input.GetKey(KeyCode.Mouse0))
    12.         {
    13.             doorRotation.GetComponent<Animator>().Play("Rotate");
    14.         }
    15.     }
    16. }
    17.  
    Screenshot 2021-10-04 220426.png Screenshot 2021-10-04 220402.png
     

    Attached Files:

  19. unity_B9E8BAC891D6341EE106

    unity_B9E8BAC891D6341EE106

    Joined:
    May 13, 2022
    Posts:
    3
    I am very new to coding so can someone uh send their WORKING code?
     
  20. unity_B9E8BAC891D6341EE106

    unity_B9E8BAC891D6341EE106

    Joined:
    May 13, 2022
    Posts:
    3
    this is mine:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class On_Click : MonoBehaviour
    {
    Animator anim;

    void Start()
    {
    anim = gameObject.GetComponent<Animator>();
    }
    void Update()
    {
    if (Input.GetMouseButtonDown(0))
    {
    anim.SetTrigger("Active");
    }
    }
    }
     
  21. unity_B9E8BAC891D6341EE106

    unity_B9E8BAC891D6341EE106

    Joined:
    May 13, 2022
    Posts:
    3
    wait nvm it doesn't work at all even if there is no errors..