I'm trying to animate a chest opening and closing, but the opening and closing animations aren't playing when they should.
I have a script attached to a chest that's meant to play an opening and closing animations when the chest is clicked and the player is in range. The debug logs show that the script is able to detect the collisions and the chest is able to tell when it's clicked on, but the animations don't play. I've both animations in the animator controller, and I've got the controller attached to the chest. No errors show up when I run the code.
using UnityEngine;
using System.Collections;
public class Chest : MonoBehaviour {
public enum ChestState
{
open,
closed,
inbetween
}
public ChestState state;
private bool touchingChest;
public GameObject entireChest;
private Animator chestAnimator;
// Use this for initialization
void Start () {
state = Chest.ChestState.closed;
touchingChest = false;
chestAnimator = entireChest.GetComponent<Animator>();
}
public void OnTriggerEnter()
{
Debug.Log("Colliding");
touchingChest = true;
}
public void OnMouseUp()
{
Debug.Log("Space");
chestAnimator.Play("Open", -1, 0f);
if (touchingChest == true)
{
if (state == ChestState.closed)
Open();
else
Close();
}
}
public void OnTriggerExit ()
{
Debug.Log("Exit");
touchingChest = false;
}
private void Open()
{
chestAnimator.Play("Open", -1, 0f);
state = ChestState.open;
}
private void Close ()
{
chestAnimator.Play("Close", -1, 0f);
state = ChestState.closed;
}
}
How do I get the opening and closing animations to play?
Answer by PunchGoat · Nov 02, 2016 at 04:57 PM
I solved the problem. I set a parameter in the animator, made transitions between the open and closed states of the chest with conditions reliant upon that parameter, and then changed my code to reference the parameter rather than the actual clip.
Your answer
Follow this Question
Related Questions
Animating in c# 1 Answer
MobController 1 Answer
AnimationOverrideController does not set clip. 1 Answer
How do I puse an animation? 1 Answer
How to set the blend tree threshold value during run time? 0 Answers