Script plays animation once per frame

I currently have a problem where an animation will be play one frame at a time if the condition is true, how would I make it play the whole animation if the condition is true.

using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.IO.Pipes;

public class animController : MonoBehaviour
{
   // private Thread t;
 
 
    private Animator anim;
    private byte[] buffer;
    // value received from pipe server


    NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "ICEClassifier", PipeDirection.InOut);


    // Use this for initialization
    // only called once
    void Start()
    {
        pipeClient.Connect();
        pipeClient.Flush();

    }
    // called once
    //Awake is called after all objects are initialized
    void Awake()
    {
        anim = GetComponent<Animator>();
        buffer = new byte[80];
    }
   
    //Update is called every frame
    // infinite loop
    void Update()
    {
        //Debug.Log(anim.GetTime());
       pipeClient.Read(buffer, 0, buffer.Length);
        Debug.Log(buffer[0]);
     
        if (buffer[0] == 4)
        {
                anim.SetBool("point", true);
        }
    }
}

Then input comes from a pipeServer and its send to this script.

have you thought about a state machine?

I am using a state machine. It works perfectly when I compare if a key has been pressed, but the focus here to grab input from another program.

thanks for the reply.

2581654--180444--upload_2016-4-4_1-0-36.png

The question comes down to that Animator window and transitions between animations. I guess what you want to have is transition’s “Has Exit Time” set to true.
http://docs.unity3d.com/Manual/class-Transition.html

I could also suggest anim.SetTrigger() instead of SetBool(). It’s a different kind of parameter that will automatically sets itself false when it runs once.

1 Like

Thanks for the reply, I thought this would work but for some reason it did not. The logic of the program is more like this

// inside the update()
// condition check an input from user
if (condition is true)
{
anim.SetTrigger(“point”);
// then play only one frame of the animation
}
// loop again and check to see if condition is true
// if true then play the second frame of the total frames
// then this continues on and on

I see, just 1 frame at the time. That needs something entirely different. I would suggest reading the available properties first

For example setting animation speed to 0 first, and then add 1 keyframe worth of time into animator’s elapsed time.

1 Like

Thank you Very much, I got it working now.