Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello !

Here's my programme :

C#
using UnityEngine;
using System.Collections;

public class Anim : MonoBehaviour {
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

		int cpt = 0;
		if (Input.GetKey ("b")) {
			cpt ++;

			if ((cpt % 2) == 0) 
				GameObject.Find ("Presse-1").animation.Stop ();

			else 
				GameObject.Find ("Presse-1").animation.Play ();
							
	}
}
}


I want to stop and start whithout restarting an animation when I use a GetKey.
Stop runs but when I use a second time the GetKey, nothing.

Could you help me please ?

Thank you :)
Posted
Updated 20-Mar-15 4:24am
v2

I assume that this will do what you want. If not, please tell, and I'll try to change it accordingly.

C#
using UnityEngine;
using System.Collections;

public class Anim : MonoBehaviour
{
    private int cpt = 0;

    // Use this for initialization
    void Start ()
    {
    }

    // Update is called once per frame
    void Update () 
    {
        if (Input.GetKey ("b"))
        {
            cpt++;

            //if ((cpt % 2) == 0)
            //    GameObject.Find ("Presse-1").animation.Stop ();
            //else
            //    GameObject.Find ("Presse-1").animation.Play ();

            var anim = GameObject.Find("Presse-1").animation;

            float speed;
            if ((cpt % 2) == 0)
                  speed = 0.0f;
            else
                  speed = 1.0f;

            foreach(var state in anim)
                  state.speed = speed;
        }
    }
}
 
Share this answer
 
v2
Comments
Coralie B 20-Mar-15 10:36am    
Thank you very much !
It runs.

But the animation restarts... I assume I make a mistake when I use "animation.Stop".
How can I make a pause ? ( a break in the animation whithout restarting).

Thank you again
[no name] 20-Mar-15 10:42am    
Animation.Stop rewinds the Animation to the start, see here:
http://docs.unity3d.com/460/Documentation/ScriptReference/Animation.Stop.html

I'm looking for something like pausing but haven't found it yet.
Coralie B 20-Mar-15 10:43am    
Ok thank you :)
[no name] 20-Mar-15 10:49am    
I think I got it: You would have to set the speed of the animation to zero. I'll update the code above, take another look in a few minutes :)
[no name] 20-Mar-15 11:22am    
Does that work?
C#
using UnityEngine;
using System.Collections;

public class Anim : MonoBehaviour
{
    private int cpt = 0;


    // Use this for initialization
    void Start ()
    {
    }

    // Update is called once per frame
    void Update ()
    {
        if (Input.GetKey ("b"))
        {
            animation["Take 001"].speed = 1.0f;
        }
        if (Input.GetKey ("n"))
        {
            animation["Take 001"].speed = 0.0f;
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900