Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to play Animator when something happens?

Discussion in 'Scripting' started by groch, May 16, 2015.

  1. groch

    groch

    Joined:
    Apr 7, 2015
    Posts:
    29
    Hi there,

    quick description:

    I have:
    - camera
    - about 20 different objects
    - destroyed versions of these objects

    I want to play an Animator in the camera (very quick zoom in and zoom out) when the object is destroyed. How can I do that?

    From what I know, the code should partly look like that:

    Code (CSharp):
    1.     Animator anim;    
    2.  
    3. if ( Here's the part, where I don't know what to put )
    4.         {
    5.             anim = GetComponent<Animator>();
    6.         }
    I know, that's a simple problem, but I'm more a painter than programmer. :d
     
    Last edited: May 16, 2015
  2. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136
    If you want to zoom in/out the camera you shouldn't be using animations. Animations are for 3d animations or sprite animations as far as I know.

    In 3D you can play around with the camera position and the field of view to make a zoom in/out. In 2D you can play around with the position and the size.
     
  3. groch

    groch

    Joined:
    Apr 7, 2015
    Posts:
    29
    Hmm, but I can animate the size of the camera using Animator and I am making a 2D game. I just don't know what I should type after "if" in brackets. I want to zoom in/out the camera when destroyed object shows up.

    EDIT:

    I guess, I can use tags with that destroyed objects, so the animation in camera would start, when the object with that tag will show up, but how the script should look like?

    something like this?

    Code (CSharp):
    1.  
    2.   Animator anim;
    3.  
    4.   if (gameObject.tag == "example")
    5.   {      
    6.      anim = GetComponent<Animator>();  
    7.   }
    8.  
     
    Last edited: May 16, 2015
  4. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136
    As I said, movement by animation is only used on specific cases. For example, when you have an animation of a player running.

    In this case, you want to zoom in and out a camera, so you'll have to do this via script and not via Animator.
     
  5. groch

    groch

    Joined:
    Apr 7, 2015
    Posts:
    29
    Are you sure? I already did it in Animator, here is the screenshot. I just need to know how in the script make it play when destroyed object shows up.

     
  6. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    You need to setup some transitions in the Animator window. Here is an example of my menu system Animator.

    AnimatorWindow.png

    I've got 2 triggers for parameters, Open and Close.
    Both animations are set to not loop.
    OpenAnim will be played once Open is triggered.
    CloseAnim will be played once Close is triggered.
    I made it so no animation gets played at the start.

    Code (CSharp):
    1. Animator anim;
    2.  
    3. void Start()//or Awake()
    4. {
    5.      anim = GetComponent<Animator>();
    6. }
    7.  
    8. void Open()
    9. {
    10.     anim.SetTrigger("Open");
    11. }
    12.  
    13. void Close()
    14. {
    15.     anim.SetTrigger("Close");
    16. }
    17.  
    This is one way to do it.
     
    groch likes this.
  7. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136
    Of course you can, but you said you want to zoom at an object, not just zoom the camera. You'll need to move the camera to the object position and then change the size, that's why you need to do it via script.
     
  8. groch

    groch

    Joined:
    Apr 7, 2015
    Posts:
    29
    No, no, I just need to zoom the camera. ;) I don't need it to zoom at an object. I want it to zoom when that object will show up just in game, I don't even need to see that object on the screen.

    Hmmm, so how the script should look like in my case? I want to activate Animator in the camera, when other object is destroyed.
     
    Last edited: May 17, 2015
  9. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136
    Oh I thought you wanted to move it to the gameObject.

    It's fine then. With the Animator component make 3 points. First one is the default, middle one should be the zoomed one and third one default again.

    When you have that, you should play around with the speed and see which animation speed looks cool.


    Lastly, when an object is destroyed, you should call Play() on the Animator component, if clip is loaded.
     
    groch likes this.
  10. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    The destroyed object needs to tell the camera it has been destroyed. There are many ways it can be done.

    You could send a message to the camera object from the object that gets destroyed.
    Code (CSharp):
    1. void OnDestroy()
    2. {
    3.     Camera.main.SendMessage("ZoomIn");
    4. }
    then in the camera script
    Code (CSharp):
    1. void ZoomIn()
    2. {
    3.     anim.SetTrigger("ZoomIn");
    4. }
     
    groch likes this.