1
\$\begingroup\$

I'm creating platforms that break on trigger. The animation works and the colliders are set like they should, but I can't seem to figure out how to trigger the "stepBreak" animation I have.


This is what I have, so far:

using UnityEngine;
using System.Collections;

public class breakablePlatforms : MonoBehaviour
{
    public GameObject steps;
    private Animator animator;

    void Start()
    {
        animator = GetComponent<Animator>();
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if(other.tag == "Player")
        {
            animator.Play("stepBreak")
        }
    }
}

These are my animations and transitions:

Transition from "stepIdle" to "stepBreak"

Transition from "stepBreak" to "stepIdle"

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to GameDev. You will notice I have converted your code to text; for future questions, this is preferable, as it allows users to quickly test anything. I have also removed an image, as I felt it greatly detracted from your question, and did not offer anything important. What your asking is fairly easy, I should be able to post a solution in a timely manner. \$\endgroup\$
    – Gnemlock
    Dec 5, 2016 at 9:52

2 Answers 2

2
\$\begingroup\$

You have a bool value on your animation state, you have to use the SetBool method.

Basically it´s playing stepIdle over and over and waiting for a if statement, if break is true play stepBreak and if break is false go back to play stepIdle

// play anim
animator.SetBool("break", true);

// exit anim and go back to idle state
animator.SetBool("break", false);

Here is a link to description https://docs.unity3d.com/ScriptReference/Animator.SetBool.html

\$\endgroup\$
2
  • \$\begingroup\$ All right I'll give that a shot and get back at yah. Thanks👍🏻 \$\endgroup\$ Dec 6, 2016 at 5:47
  • \$\begingroup\$ Please accept answer if it helped you :) \$\endgroup\$ Apr 26, 2017 at 9:34
0
\$\begingroup\$

An easy way to trigger animations by code is to set up a trigger, set it as the condition for the transition, and trigger it with animator.SetTrigger().


Setting up a Trigger

Looking at the Animator window, you should see a tabbed window to the left of the controller chart. Select the "Parameters" tab. Click on the button marked + and select "Trigger". Give it a meaningful name, and your done.

After selecting "Animator > Parameters > + > Trigger", I name my trigger "twitch".

If you are missing the "Layers / Parameters" window, check for a closed eyelid (The "Layers / Parameters" window may be folded up; click on the closed eyelid to make it display.), along the same bar. Clicking this button unfolds the window.


Setting up the Transition

On the transition to the desired animation, in your case "stepBreak" and in my case "twitch", you will find the trigger you made listed under the "Conditions" option. Select your trigger, and set it as the only condition.

Viewing the desired transition in the Inspector, set the trigger as the only condition.

Depending on your circumstance, you probably want to set up a transition in the opposite direction. Given that you are animating something being broken, I would assume you probably wouldn't transition back to the mended state. Either way, you can set (or not set) your additional transitions up however you want.


Triggering the Trigger

Now everything is set up, all you need to do is grab a reference to the specific Animator instance attached to the game object, and call animator.SetTrigger(triggerName) where triggerName is the name you gave to the trigger you just created and set up.

I've done mine using the UnityEditor namespace to give me a quick and easy button to click from the Inspector:

using UnityEngine;
using UnityEditor;

public class TriggerAnimator : MonoBehaviour 
{
    // Reference to the Animator, which I add from the Inspector.
    public Animator animator;
}

[CustomEditor(typeof(TriggerAnimator))]
public class TriggerAnimatorEditor : Editor
{
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector()
        TriggerAnimator triggerAnimator = (TriggerAnimator)target;

        // If the created button "Animate" is clicked,
        if (GUILayout.Button("Animate"))
        {
            // Set off the trigger "twitch" on the animator in triggerAnimator.
            triggerAnimator.animator.SetTrigger("twitch");
        }
    }
}

You would probably use something more simple, like this:

void OnTriggerEnter2D(Collider2D other)
{
    if(other.tag == "Player")
    {
        animator.SetTrigger("stepBreak")
    }
}

That's all there really is to it.

With one line of code, my button animates my sprite.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .